Skip to content Skip to sidebar Skip to footer

Unboundlocalerror: Local Variable 'arm' Referenced Before Assignment?

Hi everyone I'm a beginner in python and I'm trying to make my own text rpg game. I have made a method for the hero to shop in the store but for some reason I'm getting this error

Solution 1:

Problem is that when you do:

ifarm==1:# codeifarm==2:# code

you have not defined what arm is.. you only define arm in this line:

arm = int(input("enter a value: "))

Which is in an inner scope of the elif - which means that if it doesn't reach that point then arm is indeed a local variable that was not assigned before doing anything with it.

Maybe what you meant to do is that these if arm == 1: ... in the scode of the elif above I can't tell but I think you should see how you can change your code to contain less spagetti code.. deviding into functions and maybe classes.

Solution 2:

You have declared variable arm inside the elif(inner scope) and you are trying to use that variable out of that scope. Here, Same thing is happening with another variable selection.

If control would not reach up to that conditions, your variables would be undefined.

You can first declare these variable with None

defshop():
    dagger = ('Dagger', 0, 5)
    sword = ('Sword', 0, 10)
    leather_hide = ('Leather Hide', 5, 0)
    selection=None
    arm=None#rest of the code.

Post a Comment for "Unboundlocalerror: Local Variable 'arm' Referenced Before Assignment?"