fightstarr20 fightstarr20 - 7 months ago 16
Python Question

Python access variable in while loop

I have this simple Python 3 script

def myfunction(current):
current += 1
myvalue = 'Test Value'
print(current)
return current

current = 1
while ( current < 10 ):
current = myfunction(current)


It works great but I am trying to use the myvalue variable in the while loop. How can I get access to the variable?

Answer

You'll have to return myvalue if you want to use it.

def myfunction(current):
    current += 1
    myvalue = 'Test Value'
    print(current)
    return current, myvalue

current = 1
while ( current < 10 ):
    current, myvalue = myfunction(current)
    print(myvalue)
Comments