【问题标题】:game errors, not sure what I am supposed to do游戏错误,不知道该怎么办
【发布时间】:2013-11-06 10:53:00
【问题描述】:

我正在处理的程序有问题。它旨在成为一个城市模拟器。 前 2 行有效,但随后无效。代码如下。我不知道我做错了什么。

>>> ================================ 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

我已经解决了这个问题。感谢您的帮助。

【问题讨论】:

  • 您是否收到任何错误消息?你试过调试吗?

标签: python simulator globals


【解决方案1】:

您将 money 定义为全局范围内的 a,但在您的 Turn() 函数中您未能将其声明为全局。你应该这样做:

money = 2000
moneyPerTurn = population * 5

def Turn ():
    global money
    global moneyPerTurn
    money = money + moneyPerTurn
    print('STATUS REPORT')

【讨论】:

  • 非常感谢!我会使用向上箭头,但我没有足够的声誉。
【解决方案2】:

Turn 中,必须声明money 引用全局变量money

def Turn ():
    global money
    money = ........

现在 python 正在寻找在 Turn 函数中定义的 money 变量,该变量不存在。 global 关键字将变量从全局范围“导入”到 Turn 的范围内。

【讨论】:

    猜你喜欢
    • 2012-02-01
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2018-05-08
    • 2022-11-20
    • 1970-01-01
    相关资源
    最近更新 更多