-2

私が取り組んでいるプログラムに問題があります。これは、都市シミュレーターになることを目的としています。最初の 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

問題を解決しました。ご協力ありがとうございました。

4

2 に答える 2

1

moneyグローバル スコープで として定義していますが、Turn()関数ではそれをグローバルとして宣言していません。次のようにする必要があります。

money = 2000
moneyPerTurn = population * 5

def Turn ():
    global money
    global moneyPerTurn
    money = money + moneyPerTurn
    print('STATUS REPORT')
于 2013-10-27T18:33:10.593 に答える
0

では、 がグローバル変数Turnを参照することを宣言する必要があります。moneymoney

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

現在、python は関数でmoney定義された変数を探していますTurnが、存在しません。キーワードはglobal、変数をグローバル スコープから のスコープに「インポート」しますTurn

于 2013-10-27T18:32:48.987 に答える