0

This is my code, sorry it is in Swedish but it is basically rock, paper, scissors. I get the error UnboundLocalError: local variable 'Answer' referenced before assignment even though if the input is correct, the variable should be assigned.

The input has to be either rock, paper or scissors and if it's not it starts over. But even when the input is correct it says "Answer" is not assigned. Please help, have been stuck on this problem for a while. And no making it global is not an option since it has to be random each round.

def Start():
    global win
    global lost
    game = input("Skriv sten, sax eller påse: ")
    game2 = game.lower()
    notright = game2 is "sten" or "sax" or "påse"
    if game2 == "quit":
        quit
    if notright == True:
        Answer = random.randint(0,3)
        if Answer == 1:
            print ("Du valde",game2,"och Jag valde Sten")
        elif Answer == 2:
            print("Du valde",game2,"och Jag valde Sax")
        elif Answer == 3:
            print("Du valde",game2,"och Jag valde Påse")
    elif notright == False:
        print("Du måste välja sten, sax eller påse. Prova igen!")
        Start()
    if Answer == 1 and game2 == "sten":
        print("Det blir oavgjort")
    elif Answer == 1 and game2 == "sax":
        lost = True
    elif Answer == 1 and game2 == "påse":
        win = True
    elif Answer == 2 and game2 == "sten":
        win = True
    elif Answer == 2 and game2 == "sax":
        print("Det blir oavgjort")
    elif Answer == 2 and game2 == "påse":
        lost = True
    elif Answer == 3 and game2 == "sten":
        lost = True
    elif Answer == 3 and game2 == "sax":
        win = True
    elif Answer == 3 and game2 == "påse":
        print("Det blir Oavgjort")
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62

1 Answers1

0

You don't have a loop, so when you hit:

elif notright == False:

you call Start. First quit doesn't do anything, and checking if notright is False doesn't help, just do if notright:.

So the start of the loop would be:

def Start():
    while True:
        game = input("Skriv sten, sax eller påse: ")
        game2 = game.lower()
        notright = game2 == "sten" or game2 == "sax" or game2 == "påse"
        if game2 == "quit":
            break
        if !notright:
            print("Du måste välja sten, sax eller påse. Prova igen!")
            continue

        Answer = random.randint(0,3)
        if Answer == 1:
            print ("Du valde" + game2 + " och Jag valde Sten")
        elif Answer == 2:
            print("Du valde" + game2 + "och Jag valde Sax")
        elif Answer == 3:
            print("Du valde" + game2 + "och Jag valde Påse")
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62