我对正在处理的程序有疑问。它旨在成为一个城市模拟器。 前两行有效,但后来无效。代码如下。我不知道我做错了什么。
>>> ================================ 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
>>>
var = True
houses = 5
population = houses * 2
money = 2000
moneyPerTurn = population * 5
fiveFactories = 2
tenFactories = 0
twentyFiveFactories = 0
oneHundredFactories = 0
totalFactories = fiveFactories + tenFactories + twentyFiveFactories + oneHundredFactories
cinemas = 1
restaraunts = 0
banks = 1
unemployed = population - (100 * oneHundredFactories + 25 * twentyFiveFactories + 10 * tenFactories + 5 * fiveFactories)
slums = unemployed / 20
slumPeoplePercent = population / 100 * slums
richness = (restaraunts + banks * cinemas) - slums
choicevar = ''
def Turn ():
money = money + moneyPerTurn
print('STATUS REPORT')
print('TOTAL POPULATION - ' + population)
print('MONEY - ' + money + ' (' + moneyPerTurn + ' per turn)')
print('RICHNESS INDEX - ' + richness)
print('FACTORIES - ' + totalFactories)
print('CINEMAS - ' + cinemas)
print('RESTARAUNTS - ' + restaraunts)
print('BANKS -' + banks)
print('UNEMPLOYED - ' + unemployed)
print('SLUMS - ' + slums)
print('POPULATION IN SLUMS - ' + slumPeoplePercent + '%')
print('FACTORY DETAILS')
print('SMALL FACTORIES - ' + fiveFactories)
print('MEDIUM FACTORIES - ' + tenFactories)
print('LARGE FACTORIES - ' + twentyFiveFactories)
print('GIANT FACTORIES - ' + oneHundredFactories)
print('Press enter to continue.')
uselessVar = input()
print('Enter the word "house" to build a house for 50 dollars.')
print('Enter the word "smallfactory" to build a small factory for 100 dollars.')
print('Enter the word "mediumfactory" to build a medium factory for 200 dollars.')
print('Enter the word "largefactory" to build a large factory for 500 dollars.')
print('Enter the word "giantfactory" to build a giant factory for 2000 dollars.')
print('Enter the word "cinema" to build a cinema for 750 dollars.')
print('Enter the word "restaraunt" to build a restaraunt for 100 dollars.')
print('Enter the word "bank" to build a bank for 100 dollars.')
print('Enter anything else to skip the turn.')
choicevar = input()
return
def ExecuteTurn ():
if choicevar == "house":
houses = houses + 1
money = money - 50
else:
if choicevar == "smallfactory":
fiveFactories = fiveFactories + 1
money = money - 100
else:
if choicevar == "mediumfactory":
tenFactories = tenFactories + 1
money = money - 200
else:
if choicevar == "largefactory":
twentyFiveFactories = twentyFiveFactories + 1
money = money - 500
else:
if choicevar == "giantfactory":
oneHundredFactories = oneHundredFactories + 1
money = money - 2000
else:
if choicevar == "cinema":
cinemas = cinemas + 1
money = money - 750
else:
if choicevar == "restaraunt":
restaraunts = restaraunts + 1
money = money - 100
else:
if choicevar == "bank":
banks = banks + 1
money = money - 100
else:
print('Turn successfully skipped.')
return
print ('Welcome to SIM')
print ('This is a text based city simulator.')
while var == True:
Turn()
ExecuteTurn()
EDIT
我已经解决了这个问题。谢谢你的帮助。
答案 0 :(得分:1)
您将money
定义为全局范围,但在Turn()
函数中,您未能将其声明为全局范围。你应该这样做:
money = 2000
moneyPerTurn = population * 5
def Turn ():
global money
global moneyPerTurn
money = money + moneyPerTurn
print('STATUS REPORT')
答案 1 :(得分:0)
在Turn
中,您必须声明money
引用全局money
变量:
def Turn ():
global money
money = ........
现在,python正在寻找money
函数中定义的Turn
变量,该变量不存在。 global
关键字将全局范围内的变量“导入”Turn
的范围。