我是Python新手
我如何才能完成这样的任务:
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
我使用全局变量的实际代码:
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
如何在不使currentNum
全局化的情况下实现这一点(从otherfunc
访问和更改currentNum
)
如果您想访问
otherfunc
中的currentNum
,应该将其传递给该函数。如果您想要otherfunc
更改它,只需让它返回一个更新版本即可。请尝试以下代码:相关问题 更多 >
编程相关推荐