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

17th Feb 2020, 9:54 PM
Ryan Bartels
Ryan Bartels - avatar
3 Respuestas
+ 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
17th Feb 2020, 10:18 PM
vad lensky
vad lensky - avatar
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.
17th Feb 2020, 10:35 PM
vad lensky
vad lensky - avatar