Menu Close

How to fix function not changing global variable with Python?

Sometimes, we want to fix function not changing global variable with Python.

In this article, we’ll look at how to fix function not changing global variable with Python.

How to fix function not changing global variable with Python?

To fix function not changing global variable with Python, we add the global keyword in front of the letter we want to change.

For instance, we write

def function():
    global done
    for loop:
        code
        if not comply:
            done = True

to define the done variable with global.

Then in the if block, we set the outside done variable to True since we have global in front of done.

Conclusion

To fix function not changing global variable with Python, we add the global keyword in front of the letter we want to change.

Posted in Python, Python Answers