-1

I have a problem with the python code. Indeed, I want to change the value of a global variable through a function like this:

myVar = True # default value
def myFunct():
   myVar = False

The problem is with Python, you can't change the value of the global variable by a function. So anyone have a solutuion ?

Thank you.

Julien Pepe
  • 142
  • 8

1 Answers1

0
myVar = True # default value
def myFunct():
   global myVar
   myVar= False

myFunct()  
print(myVar)

Output:

False

Explanation:

When you want to access the global variable inside a function access that variable using "global" keyword. And then you can update that variable.