【问题标题】:TypeError: unsupported operand int and NoneType? PythonTypeError:不支持的操作数 int 和 NoneType? Python
【发布时间】:2013-12-17 13:53:26
【问题描述】:

我正在为我的班级编写一个二十一点游戏,但我似乎根本无法弄清楚代码有什么问题,也许我只是盯着它太久了,但我们将不胜感激

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)

接下来是:

winning=payout(bet)
playerPool+=winning

抱歉,完整代码是:

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

实际的错误是这样的:

Traceback(最近一次调用最后一次): 文件“C:\Users\Daniel\Desktop\Software Design\Culminating project revision2.py”,第 175 行,在 main()

文件“C:\Users\Daniel\Desktop\Software Design\Culminating project revision2.py”,第 140 行,在 main playerPool+=获胜

TypeError:+= 不支持的操作数类型:“int”和“NoneType”

【问题讨论】:

  • 完整显示错误信息。事实上,将所有代码完整地展示给我们可能是个好主意。
  • 除了忘记= 为betAmount *= 1.5 行之外,您的代码应该可以正常工作,除非bet 是None。或option1 不是您测试的字符串值。
  • 请告诉我们bet和option1。
  • option1 是一个糟糕的变量名,你应该将它作为第二个参数传递给这个函数,而不是使用 global
  • 请记住,当您寻求帮助时,请向人们提供他们需要的所有信息,以便轻松解决问题。事实上,您没有说明错误发生的位置(错误消息告诉您这一点),我们知道您是如何创建 bet 变量的,所以我们不知道它是什么类型的对象。

标签: python typeerror nonetype


【解决方案1】:

当您在 payoutRules 中设置 option1 时,您会分配 整数 值。

当您在payout 中检查option1 时,您会将其与字符串 进行比较。它不能匹配任何东西,因此payout 总是返回None。

您可以将 payout 函数更改为:

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

这样做你使用option1,它是一个整数,可以索引到(1.5, 1.25, 1.2) 元组中,以使乘数与betAmount 相乘。如果option1 不是整数,您将在此处收到错误消息。

【讨论】:

  • 感谢您的帮助!我确实忽略了那个,实际上我没有更改函数,而是在我的 payout() 函数中去掉了引号,因为它似乎以相同的方式工作。我想我只是需要另一双眼睛来看看它
【解决方案2】:

如果option1 既不是'1' 也不是'2' 也不是'3',您的函数将返回None。在这种情况下,将winning(这是payout 的结果)添加到playerPool 将会失败。

也许你添加一个print(option1) 作为你的函数的第一行,看看它是什么样子的。

你可以这样重构你的函数:

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

当option1 不是有效选项时,这样您至少会得到一个关键错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-22
    • 2017-12-27
    • 2014-03-31
    • 2021-05-23
    • 2015-10-15
    • 2016-08-25
    • 2011-10-28
    • 1970-01-01
    相关资源
    最近更新 更多