global a # Let global variable be a
a = None
def test():
a = 2 #Assigned the value 2
print(a) #The result of this is 2 (Which I desire)
def test2:
print(a) #The result is None (But I expect it to be 2)
# Please Help me with this
Asked
Active
Viewed 26 times
0

khelwood
- 55,782
- 14
- 81
- 108

Sachin Jindal
- 11
- 3
-
Put the global statement in `test`. – qouify Aug 05 '21 at 09:13
-
You need to put "global a" on the row over a = 2 – Sefan Aug 05 '21 at 09:18
-
I want global variable outside function only. Thanks for suggestions – Sachin Jindal Aug 06 '21 at 07:28
-
If you want your program to work, you need to put statements in the places that the language requires them. Putting a global statement at the top of your program is pointless. – khelwood Aug 06 '21 at 07:40