Search code examples
pythonpycharmtypeerrornonetype

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


I'm very new to python. I tried to create a rock paper scissors game but I received the error:

TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'

on the line: dif = a - b

I tried searching for the solution on Google and Stackoverflow and nearly all answers I found said it had to something with changing print to return. I tried to do that on several places but I ended up getting more errors, so therefore I'm asking this question.

Does anyone know how to solve this error for this particular code? Thanks!!

Code:

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

Solution

  • So I tried some more things and I think I know why I received the error. I think I put one extra space before the answer as my input. So instead of answering "rock", I answered " rock" Therefore there was no integer value assigned to my input as " rock" is not in my dictionary which made the dif line give me an error.

    For anyone who might come across the same problem, this is what I did so that I don't receive an error if the input is slightly off (it will now tell the user that the input was not correct and they should try something else):

    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
    

    So first of all, I created a list with the possible answers, called antwOpties. I then created a piece of code that would check whether the input of player 1 and 2 was in that list. If that's not the case, it prints that out and asks for other input and then returns back to the beginning of the loop thanks to the "continue". And at last I moved the "dif = a - b" below that piece of code which checks whether the input is valid. I did that so that it doesn't go through that if the input is not valid (as it only goes past the "continue" if the input IS in the answers and thus corresponds to an integer.