Problem with Variables | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Problem with Variables

I want to change the global a Why Error? a = 4 def func(): print(a) a += 2 func() https://code.sololearn.com/cOQ3t7h3DllB/?ref=app

1st Apr 2021, 6:25 AM
Carlos
Carlos - avatar
4 Answers
+ 1
this is yours: a = 4 def func(): print(a) a += 2 func() And this is one of the many right ones: a = 4 def func(x): print(x+2) func(a) And if you want to affect the variable outside of the function scope, then: a = 4 def func(): global a print(a) a += 2 func()
1st Apr 2021, 6:30 AM
Shadoff
Shadoff - avatar
+ 3
Insert in func dimension "global a" https://code.sololearn.com/cjhL2EYSl5j8/?ref=app
1st Apr 2021, 6:41 AM
Илья Мирошник
Илья Мирошник - avatar
+ 2
a=4 def func(a): print(a) a += 2 func(a) I think 🤔 it's showing an error because you didn't called the argument.
1st Apr 2021, 6:30 AM
Kǟrɨsɦmǟ ❥
Kǟrɨsɦmǟ ❥ - avatar
0
you print the value of a variable that does not exist in the function
1st Apr 2021, 6:31 AM
Alfonso Farías
Alfonso Farías - avatar