Categories
Python Answers

How to fix function not changing global variable with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *