Question

I have an issue with a program i am working on. It is intended to be a city simulator. The first 2 lines work, but then it does not work. The code is below. I have no idea what I am doing wrong.

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

The code

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

I have solved the issue. Thank you for your help.

Was it helpful?

Solution

You're defining money as a in the global scope, but in your Turn() function you're failing to declare it as global. You should do something like this:

money = 2000
moneyPerTurn = population * 5

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

OTHER TIPS

In Turn, you must declare that money refers to the global money variable:

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

Right now python is looking for a money variable defined in the Turn function, which doesn't exist. The global keyword 'imports' the variable from the global scope into the scope of Turn.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top