Python 游戏出错,不知道我该怎么做
我对我正在做的一个项目有意见。它旨在成为一个城市模拟器。 前两行可以工作,但之后就不工作了。代码如下。我不知道我做错了什么Python 游戏出错,不知道我该怎么做,python,simulator,globals,Python,Simulator,Globals,我对我正在做的一个项目有意见。它旨在成为一个城市模拟器。 前两行可以工作,但之后就不工作了。代码如下。我不知道我做错了什么 >>> ================================ RESTART ================================ >>> Welcome to SIM This is a text based city simulator. Traceback (most recent call last):
>>> ================================ RESTART ================================
>>>
Welcome to SIM
This is a text based city simulator.
Traceback (most recent call last):
File "C:/Python33/SIM.py", line 102, in <module>
Turn()
File "C:/Python33/SIM.py", line 25, in Turn
money = money + moneyPerTurn
UnboundLocalError: local variable 'money' referenced before assignment
>>> ================================ RESTART ================================
>>>
Welcome to SIM
This is a text based city simulator.
Traceback (most recent call last):
File "C:/Python33/SIM.py", line 102, in <module>
Turn()
File "C:/Python33/SIM.py", line 25, in Turn
money = money + moneyPerTurn
UnboundLocalError: local variable 'money' referenced before assignment
我已经解决了这个问题。谢谢您的帮助。在
回合
中,您必须声明货币
指的是全局货币
变量:
def Turn ():
global money
money = ........
现在python正在寻找
Turn
函数中定义的money
变量,该变量不存在。global
关键字将变量从全局范围“导入”到Turn
的范围中,您将money
定义为全局范围中的,但在Turn()
函数中,您未能将其声明为全局变量。你应该这样做:
money = 2000
moneyPerTurn = population * 5
def Turn ():
global money
global moneyPerTurn
money = money + moneyPerTurn
print('STATUS REPORT')
您是否收到任何错误消息?你试过调试吗?非常感谢!我会使用向上箭头,但我没有足够的声誉。
money = 2000
moneyPerTurn = population * 5
def Turn ():
global money
global moneyPerTurn
money = money + moneyPerTurn
print('STATUS REPORT')