使用If条件时Python中的NoneType错误

Posted

技术标签:

【中文标题】使用If条件时Python中的NoneType错误【英文标题】:NoneType Error in Python while using If condition 【发布时间】:2016-08-10 21:47:56 【问题描述】:

我无法让我在 python 中的一个函数工作。我的函数代码如下:

def checkBlackjack(value, pot, player, wager):
    if (value == 21):
        print("Congratulations!! Blackjack!!")
        pot -= wager
        player += wager
        print ("The pot value is $", pot)
        print ("Your remaining balance is $",player)
        return (pot, player)

函数调用是:

potValue, playerBalance = checkBlackjack(playerValue, potValue, playerBalance, wager)

我得到的错误是:

potValue, playerBalance = checkBlackjack(playerValue, potValue, playerBalance, wager)
TypeError: 'NoneType' object is not iterable

由于错误涉及无法迭代,我不确定如何将其与使用 if 条件联系起来。

任何帮助将不胜感激。谢谢!

【问题讨论】:

你应该问问自己当value 不是21 时会发生什么。 【参考方案1】:

只有在满足函数中的条件时才会返回某些内容,否则函数默认返回 None,然后它会尝试将 None 解压缩为两个值(您的变量)

【讨论】:

【参考方案2】:

这里是MCVE 这个问题:

>>> a, b = None
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    a, b = None
TypeError: 'NoneType' object is not iterable

至此,问题应该很清楚了。如果没有,可以在手册中查找多项作业。

【讨论】:

以上是关于使用If条件时Python中的NoneType错误的主要内容,如果未能解决你的问题,请参考以下文章