I'm writing a blackjack game for my class and I cannot seem to figure out what is wrong with the code at all, maybe I'm just staring at it too long but any help would be appreciated

def payout(betAmount):
    global option1
    if option1 == "1":
        betAmount*1.5
        return int(betAmount)
    elif option1 == "2":
        betAmount*=1.25
        return int(betAmount)
    elif option1 == "3":
        betAmount*=1.2
        return int(betAmount)

followed by this:

winning=payout(bet)
playerPool+=winning

sorry about that, the full code is:

import random, pickle

option1=None

def payoutRule():
    global option1
    pick=None
    while pick != "1" or "2" or "3":
        pick=input("""
What is the house payout you want to play with?

1- 3:2 (1.5X)
2- 5:4 (1.25X)
3- 6:5 (1.2X)\n
""")

        if pick == "1":
            option1=1
            break
        elif pick == "2":
            option1=2
            break
        elif pick == "3":
            option1=3
            break
        else:
            print("That is not a valid option!\n")

def payout(betAmount):
    global option1
    if option1 == "1":
        betAmount*1.5
        return int(betAmount)
    elif option1 == "2":
        betAmount*=1.25
        return int(betAmount)
    elif option1 == "3":
        betAmount*=1.2
        return int(betAmount)

def hitDeck():
    CARDS=(2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11)

    drawCard=random.choice(CARDS)
    return drawCard

def aceCounter(hand):
    aces=hand.count(11)
    total=sum(hand)
    if total > 21 and aces > 0:
        while aces > 0 and total > 21:
            total-=10
            aces-=1
    return total


def main():
    print("\t\tWelcome to the Blackjack Table!")
    payoutRule()
    playerPool=int(250)

    while True:
        playerCard=[]
        dealerCard=[]

        playerBust=False
        dealerBust=False
        endGame=None
        bet=None

        playerCard.append(hitDeck())
        playerCard.append(hitDeck())
        print("You have", playerPool, "dollars to play with.")

        while True:
            bet=input("""
How much are you betting?
\t1- $5
\t2- $10
\t3- $25
""")
            if bet == "1":
                print("You put down $5 on the table.")
                bet=int(5)
                break
            elif bet == "2":
                print("You put down $10 on the table.")
                bet=int(10)
                break
            elif bet == "3":
                print("You put down $25 on the table.")
                bet=int(25)
                break
            else:
                print("That is not a valid choice!")

        while endGame != "1":
            totalPlayer=aceCounter(playerCard)
            print("You have", len(playerCard), "cards with a total value of", totalPlayer)
            if totalPlayer > 21:
                print("You are busted!")
                playerBust=True
                break
            elif totalPlayer == 21:
                print("You've got a blackjack!!!")
                break
            else:
                hit=input("""
Would you like to:
\t1- Hit
\t2- Stand
""")
                if "1" in hit:
                    playerCard.append(hitDeck())
                elif "2" in hit:
                    break
                else:
                    print("That is not a valid choice!")
        while True:
            dealerCard.append(hitDeck())
            dealerCard.append(hitDeck())
            while True:
                totalDealer=aceCounter(dealerCard)
                if totalDealer  <= 17:
                    dealerCard.append(hitDeck())
                else:
                    break
            print("The dealer has", len(dealerCard), "cards with a total value of", totalDealer)
            if totalDealer > 21:
                print("The dealer busted!")
                dealerBust=True
                if playerBust == False:
                    winning=payout(bet)
                    print("You won!")
                    print("You just won", winning, "dollars!")
                    playerPool+=winning
                elif playerBust == True:
                    print("You both busted but the house will still collect.")
                    print("You just lost", bet, "dollars...")
                    playerPool-=bet
            elif totalPlayer > totalDealer:
                if playerBust == False:
                    winning=payout(bet)
                    print("You won!")
                    print("You just won", winning, "dollars!")
                    playerPool+=winning
                if playerBust == True:
                    print("The dealer won!")
                    print("You just lost", bet, "dollars...")
                    playerPool-=bet
            elif totalPlayer == totalDealer:
                print("It's a draw, but the house still wins.")
                print("You just lost", bet, "dollars...")
                playerPool-=bet
            elif totalPlayer < totalDealer:
                print("The dealer won!")
                print("You just lost", bet, "dollars...")
                playerPool-=bet
            break

        if playerPool == 0:
            print("You don't have anymore money to play with!")
            break
        elif playerPool < 0:
            print("You owe the house", -playerPool, "dollars, time to work in the kitchen...")
            break
        print("You have", playerPool, "dollars in your pocket now.")
        playAgain=input("Press the Enter key to play again or 'q' to quit.\n")
        if "q" in playAgain.lower():
            break

The actual error is this:

Traceback (most recent call last): File "C:\Users\Daniel\Desktop\Software Design\Culminating project revised2.py", line 175, in main()

File "C:\Users\Daniel\Desktop\Software Design\Culminating project revised2.py", line 140, in main playerPool+=winning

TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'

有帮助吗?

解决方案

When you set option1 in payoutRules you assign integer values.

When you check option1 in payout you compare it against strings. It cannot match anything, and thus payout always returns None.

You could change your payout function to:

def payout(betAmount):
    return betAmount * (1.5, 1.25, 1.2)[option1]

Doing it this way you use option1, which is an integer to index into the (1.5, 1.25, 1.2) tuple to get the multiplier to multiply with betAmount. If option1 is not an integer, you'll get an error right there.

其他提示

Your function will return None if option1 is neither '1' nor '2' nor '3'. In this case adding winning (which is the result of payout) to playerPool will fall.

Maybe you add a print(option1) as first line of your function, to see what it looks like.

You can refactor your function this way:

def payout(betAmount, option1):
    return int({'1': 1.5, '2': 1.24, '3': 1.2}[option1] * betAmount)

This way you get at least an key error, when the option1 is not a valid option.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top