我正在尝试创建一个脚本来设置一个局部变量,从一个函数中引用它,并且可以将操纵值返回到主范围(或者它被称为什么;我是 Python 新手)
我已经简化了我的代码,以展示我想要完成的最基础的工作,我认为是将模块中的本地导入到功能块中。
我已经通过使用来实现这一点globals
,但这不是最好的解决方案。. .
chambersinreactor = 0;
cardsdiscarded = 0;
def find_chamber_discard():
"""Find chambers and discard in row (reads each player slot)"""
chambersinreactor = 0; # Resets the variable, not what I want
cardsdiscarded = 0; # Resets the variable, not what I want
chambersinreactor += 1
cardsdiscarded += 1
return # Don't know what to put here
find_chamber_discard()
print chambersinreactor # prints as 0, should be 1
print cardsdiscarded # prints as 0, should be 1