Reputation: 2399
I am new to Python.
How can I accomplish something like this:
def gameon():
currentNum = 0
for x in range(100):
currentNum+=1
otherfunc()
def otherfunc(maybe a possible parameter...):
for y in range(500):
#check for some condition is true and if it is...
#currentNumFROMgameon+=1
My actual code that used global variables:
def gameon():
global currentNum
currentNum = 0
for x in range(100):
currentNum+=1
otherfunc()
def otherfunc():
global currentNum
for y in range(500):
if(...):
currentNum+=1
global currentNum
How can I accomplish this(accessing and changing currentNum
from otherfunc
) without making currentNum
global?
Upvotes: 0
Views: 33
Reputation: 4186
If you want to have access to currentNum
in otherfunc
you should pass it to that function. If you want otherfunc
to change it, simply have it return an updated version. Try this code:
def gameon():
currentNum = 0
for x in range(100):
currentNum+=1
currentNum = otherfunc(currentNum)
def otherfunc(currentNumFROMgameon):
for y in range(500):
if True: # check your condition here, right now it's always true
currentNumFROMgameon+=1
return currentNumFROMgameon
Upvotes: 1