0
Hey can someone explain why this doesn't work
Hi I'm pretty new to python and I'm wrighting a program and from some reason the following code doesn't work strength = 0 strategy = 0 speed = 0 Â def upgrade (): Â y = input ("what would you like to improve \n1:STRENGTH\n2:SPEED\n3:STRATEGY\n") Â Â if y == "1": Â Â Â strenght = strength + 1 Â elif y == "2": Â Â Â speed = speed + 1 Â elif y == "3": Â Â Â strategy = strategy + 1Â Â time.sleep (1.2) upgrade () It keeps saying that "local variable 'speed' referenced before assignment" Can someone please explain I have rewriten the code and used 2 different programs to check
3 Answers
+ 1
I guess one would have to define global variables as such in the body of the function before referring to them inside it:
def upgrade():
global strength, speed, strategy
0
Also, there seems to be a typo in the first if block, "strengHT" instead of "strength". I guess that's why it only starts complaining when speed is referenced.