How to solve: TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'?
How to solve: TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'?
我对 python 很陌生。我尝试创建剪刀石头布游戏,但收到错误消息:
TypeError: unsupported operand type(s) for -: 'NoneType' and
'NoneType'
上线:
dif = a - b
我尝试在 Google 和 Whosebug 上搜索解决方案,我找到的几乎所有答案都说它必须将打印更改为 return。我尝试在多个地方执行此操作,但最终出现了更多错误,因此我提出了这个问题。
有谁知道如何解决这个特定代码的错误?
谢谢!!
代码:
while True:
dictionary = {"steen": 1, "papier": 2, "schaar": 3}
p1 = raw_input("Maak een keuze: steen, papier of schaar:")
p2 = raw_input("Maak een keuze: steen, papier of schaar:")
a = dictionary.get(p1)
b = dictionary.get(p2)
dif = a - b
if dif in [1, -2]:
print ("Speler 1 heeft gewonnen")
if str(input("Wilt u nog een keer spelen, ja of nee?")) == "Ja":
continue
else:
print ("Game over")
break
elif dif in [-1, 2]:
print ("Speler 2 heeft gewonnen")
if str(input("Wilt u nog een keer spelem, ja of nee?")) == "Ja":
continue
else:
print ("Game over")
break
else:
print ("Gelijkspel")
if str(input("Wilt u nog een keer spelen, ja of nee?")) == "Ja":
continue
else:
print ("Game over")
break
行a = dictionary.get(p1)
大概returnsNone。或者之后的那一行。
我建议使用调试器,并在故障线上暂停。
所以我尝试了更多的东西,我想我知道我收到错误的原因。
我想我在答案前加了一个 space 作为我的输入。因此,我没有回答 "rock",而是回答了“rock” 因此没有为我的输入分配整数值,因为“rock”不在我的字典中,这使得 dif 行给我一个错误。
对于可能遇到相同问题的任何人,这就是我所做的,这样我就不会在输入略有偏差时收到错误消息(现在它会告诉用户输入不正确,他们应该尝试别的东西):
while True:
dictionary = {"steen": 1, "papier": 2, "schaar": 3}
p1 = raw_input("Speler 1, maak een keuze: steen, papier of schaar: ")
p2 = raw_input("Speler 2, maak een keuze: steen, papier of schaar: ")
a = dictionary.get(p1)
b = dictionary.get(p2)
antwOpties = ["steen", "papier", "schaar"]
if p1 not in antwOpties or p2 not in antwOpties:
print ("U heeft een ongeldig antwoord ingevuld, kies schaar, steen of papier")
continue
dif = a - b
if dif in [1, -2]:
print ("Speler 1 heeft gewonnen")
if str(input("Wilt u nog een keer spelen, ja of nee?")) == "Ja":
continue
else:
print ("Game over")
break
elif dif in [-1, 2]:
print ("Speler 2 heeft gewonnen")
if raw_input("Wilt u nog een keer spelem, ja of nee?") == "Ja":
continue
else:
print ("Game over")
break
else:
print ("Gelijkspel")
if raw_input("Wilt u nog een keer spelen, ja of nee?") == "Ja":
continue
else:
print ("Game over")
break
所以首先,我创建了一个包含可能答案的列表,称为 antwOpties。然后我创建了一段代码来检查玩家 1 和 2 的输入是否在该列表中。如果不是这种情况,它会打印出来并请求其他输入,然后 returns 返回到循环的开头,这要归功于 "continue"。最后,我将 "dif = a - b" 移到检查输入是否有效的那段代码下方。我这样做是为了在输入无效时它不会通过(因为如果输入在答案中并且因此对应于整数,它只会通过 "continue"。
我对 python 很陌生。我尝试创建剪刀石头布游戏,但收到错误消息:
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'
上线: dif = a - b
我尝试在 Google 和 Whosebug 上搜索解决方案,我找到的几乎所有答案都说它必须将打印更改为 return。我尝试在多个地方执行此操作,但最终出现了更多错误,因此我提出了这个问题。
有谁知道如何解决这个特定代码的错误? 谢谢!!
代码:
while True:
dictionary = {"steen": 1, "papier": 2, "schaar": 3}
p1 = raw_input("Maak een keuze: steen, papier of schaar:")
p2 = raw_input("Maak een keuze: steen, papier of schaar:")
a = dictionary.get(p1)
b = dictionary.get(p2)
dif = a - b
if dif in [1, -2]:
print ("Speler 1 heeft gewonnen")
if str(input("Wilt u nog een keer spelen, ja of nee?")) == "Ja":
continue
else:
print ("Game over")
break
elif dif in [-1, 2]:
print ("Speler 2 heeft gewonnen")
if str(input("Wilt u nog een keer spelem, ja of nee?")) == "Ja":
continue
else:
print ("Game over")
break
else:
print ("Gelijkspel")
if str(input("Wilt u nog een keer spelen, ja of nee?")) == "Ja":
continue
else:
print ("Game over")
break
行a = dictionary.get(p1)
大概returnsNone。或者之后的那一行。
我建议使用调试器,并在故障线上暂停。
所以我尝试了更多的东西,我想我知道我收到错误的原因。 我想我在答案前加了一个 space 作为我的输入。因此,我没有回答 "rock",而是回答了“rock” 因此没有为我的输入分配整数值,因为“rock”不在我的字典中,这使得 dif 行给我一个错误。
对于可能遇到相同问题的任何人,这就是我所做的,这样我就不会在输入略有偏差时收到错误消息(现在它会告诉用户输入不正确,他们应该尝试别的东西):
while True:
dictionary = {"steen": 1, "papier": 2, "schaar": 3}
p1 = raw_input("Speler 1, maak een keuze: steen, papier of schaar: ")
p2 = raw_input("Speler 2, maak een keuze: steen, papier of schaar: ")
a = dictionary.get(p1)
b = dictionary.get(p2)
antwOpties = ["steen", "papier", "schaar"]
if p1 not in antwOpties or p2 not in antwOpties:
print ("U heeft een ongeldig antwoord ingevuld, kies schaar, steen of papier")
continue
dif = a - b
if dif in [1, -2]:
print ("Speler 1 heeft gewonnen")
if str(input("Wilt u nog een keer spelen, ja of nee?")) == "Ja":
continue
else:
print ("Game over")
break
elif dif in [-1, 2]:
print ("Speler 2 heeft gewonnen")
if raw_input("Wilt u nog een keer spelem, ja of nee?") == "Ja":
continue
else:
print ("Game over")
break
else:
print ("Gelijkspel")
if raw_input("Wilt u nog een keer spelen, ja of nee?") == "Ja":
continue
else:
print ("Game over")
break
所以首先,我创建了一个包含可能答案的列表,称为 antwOpties。然后我创建了一段代码来检查玩家 1 和 2 的输入是否在该列表中。如果不是这种情况,它会打印出来并请求其他输入,然后 returns 返回到循环的开头,这要归功于 "continue"。最后,我将 "dif = a - b" 移到检查输入是否有效的那段代码下方。我这样做是为了在输入无效时它不会通过(因为如果输入在答案中并且因此对应于整数,它只会通过 "continue"。