197k views
3 votes
I am SOOOOO confused!!! PLEASE HELP.

I have a global variable that I want to change and save in a function (in python). I have an if statement contained in a while loop where the code will pass the if and go to the elif part first, and runs the code with certain numbers, and changes some of them. Because of the changed numbers when the while loop runs again it should enter the if part, but it doesn't It uses the old values as if the function has not been run. FYI I am returning the values at the end of the function, it still doesn't work. Is it even possible?

User Shermano
by
8.6k points

1 Answer

5 votes

Answer:

It is possible!

Step-by-step explanation:

Good luck!


























my_global_variable = 0

def my_function():

global my_global_variable

my_global_variable = 1

my_function()

print(my_global_variable) # Output: 1

User Aebabis
by
7.6k points

Related questions

asked Oct 7, 2024 138k views
Brad Reed asked Oct 7, 2024
by Brad Reed
7.8k points
1 answer
0 votes
138k views
asked May 5, 2024 5.9k views
Matey asked May 5, 2024
by Matey
8.1k points
1 answer
1 vote
5.9k views