When you are using functions, you might have noticed that you can't access that have been initialized inside a function, on the outside. Think of it like this. Whatever goes on inside a private place like your home, no one from the outside can know while whatever goes on in a public place, everyone will know. Whatever goes on from inside a private place is like a local variable while things that happen in public are like global variables. So you think you can change or access global variables inside a function just like in the main code but there is a problem. Python thinks you are creating a new variable in the same function. Here is an example of this issue:
To fix this you can use the global keyword so that Python knows that you're trying to access a global variable and not creating a new variable with the same name. Here is an example:
Warning:
It is not advised to use too many global variables as it is hard to manage and can clutter the global namespace leading to potential issues.
Copyright © 2024 PieceOfAPie - All Rights Reserved.
Powered by GoDaddy
We use cookies to analyze website traffic and optimize your website experience. By accepting our use of cookies, your data will be aggregated with all other user data.