Why is it that a variable declared inside the main block is not visible to a function declared inside that same main block?
And what should I do to make code that is structured like this work as intended?
if __name__ == "__main__":
foo = 0
def bar():
foo +=1
bar()
Traceback (most recent call last):
File "C:\Users\Mark\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-3-fa3c4c1d4159>", line 5, in <module>
bar()
File "<ipython-input-3-fa3c4c1d4159>", line 4, in bar
foo +=1
UnboundLocalError: local variable 'foo' referenced before assignment