Functions and scope of variables
Hey, comrades! I would appreciate if somebody helps me to understand the following trick with scope of variables. I took the code from quiz lib. Take a look: x=5 def foo(): print(x) pass foo() The output is 5 actually. Right? Yes, it is a correct answer. I checked it in playground. So, the meaning is we can access "x" inside the function "foo". But if we add some operations on "x" inside the function "foo" like following for example: x=5 def foo(): x += 1 print(x) pass foo() https://code.sololearn.com/c9X499z8dN0Y we get an error "UnboundLocalError: local variable 'x' referenced before assignment"! I am breaking my head on the question: why in the first code we can access the variable "x" and can't in the second example?? I am in a stuck.