+ 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:)

15th Apr 2019, 10:28 PM
tristach605
tristach605 - avatar
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
15th Apr 2019, 10:32 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 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"
16th Apr 2019, 2:54 AM
Anna
Anna - avatar
+ 1
Thanks everyone!
16th Apr 2019, 10:22 PM
tristach605
tristach605 - avatar