繁体   English   中英

如何解决:TypeError:不支持的操作数类型-:'NoneType'和'NoneType'?

[英]How to solve: TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'?

我对python很陌生。 我试图创建一个石头剪刀布游戏,但我收到错误:

TypeError: 不支持的操作数类型 -: 'NoneType' 和 'NoneType'

上线:dif = a - b

我尝试在 Google 和 Stackoverflow 上搜索解决方案,几乎所有我找到的答案都说它必须改变打印才能返回。 我尝试在几个地方这样做,但最终出现了更多错误,因此我问了这个问题。

有谁知道如何解决此特定代码的此错误? 谢谢!!

代码:

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)可能返回 None。 或者在那之后的线。

我建议使用调试器,并在故障线路上暂停。

所以我尝试了更多的东西,我想我知道为什么我会收到错误。 我想我在答案前多加了一个空格作为我的输入。 因此,我没有回答“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 的输入是否在该列表中。 如果不是这种情况,它会打印出来并要求其他输入,然后由于“继续”返回到循环的开头。 最后,我将“dif = a - b”移到了检查输入是否有效的那段代码下方。 我这样做是为了在输入无效时它不会通过(因为如果输入是在答案中,它只会通过“继续”,因此对应于一个整数。

我是python的新手。 我尝试创建剪刀石头布游戏,但收到错误消息:

TypeError:-:“ NoneType”和“ NoneType”的不受支持的操作数类型

在线上:dif = a-b

我尝试在Google和Stackoverflow上搜索解决方案,发现的几乎所有答案都表明它必须要更改打印才能返回。 我尝试在多个地方执行此操作,但最终出现了更多错误,因此我要问这个问题。

有谁知道如何解决此特定代码的错误? 谢谢!!

代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM