Here is a short example of how to use a global variable in python.
Example:
If you want to use global_answer in somefunc, you need to tell python that global_answer is a global variable.
def somefunc():
# mark global_answer as global variable
global global_answer
while global_answer is "global":
global_answer = "Will show"
# not_global is a local variable
not_global = "Will not show"
# Declare variable
global_answer = "global"
not_global = "not global"
somefunc()
print("Global:", global_answer)
print("Not Global:", not_global)
Error Reproduction
I achieve the same error message with that code:
def somefunc():
while global_answer is "global":
global global_answer # Line moved
global_answer = "Will show"
# Declare variable
global_answer = "global"
somefunc()
Possible Solution
Move your code line global answer
to the first line of your method.
Check if you defined all your classes before you use them, else you can also end up with a NameError. more details
The error might be outside of the code you provided. Where do you declare the global variable answer?