+ 2
x = 5?
x = 5 def (foo) x=x print(x) foo() Output: Error #I remember that we cannot produce undefined things, but as x = 5 and x=x (so 5) shouldn't the output be 5? Thanks for any help:)
3 Réponses
+ 5
They are in different scopes. One is in the function scope while the other is in the global scope. To make the output 5 one can add this line to the function:
global x
+ 5
You can use a global variable in your function, but you cannot change it.
x = 5 # global variable
def f():
print(x) # ok
def g():
x += 1 # error, need to use "global x"
+ 1
Thanks everyone!