I am trying to create a beginner level program to better understand programming in python. A simple while true loop that adds a value of 1 to X and prints "help" or "doing something" depending on if x is less than or greater than 10; and then breaks when x is greater than 20. Im also attempting to add in a keyboard interrupt as well to break the loop if its not too complicated.. Any tips help, I get an error
Traceback (most recent call last):
File "so.py", line 23, in <module>
help()
File "so.py", line 11, in help
x += 1
UnboundLocalError: local variable 'x' referenced before assignment
Code:
import time
x = 1
try:
def help():
print("Help.")
time.sleep(2)
x += 1
def doStuff():
print("Doing Stuff")
time.sleep(2)
x += 1
while True:
if x < 10:
help()
elif x < 20 and x > 10:
doStuff()
else:
break
except KeyboardInterrupt:
exit()