12

I am getting this error and I've read other posts but they say to put global before dollars = 0 which produces a syntax error because it doesn't allow the = 0. I'm using dollars as a counter so I can keep track of what I add to it and display it back when needed.

dollars = 0

def sol():
    print('Search or Leave?')
    sol = input()
    if sol == 'Search':
        search()
    if sol == 'Leave':
        leave()

def search():
    print('You gain 5 bucks')
    dollars = dollars + 5
    shop()

def leave():
    shop()

def shop():
    shop = input()
    if shop == 'Shortsword':
        if money < 4:
            print('I\'m sorry, but you don\'t have enough dollars to buy that item.')
            shop1()
        if money > 4:
            print('Item purchased!')
            print('You now have ' + dollars + ' dollars.')

sol()

Error message:

Traceback (most recent call last):
  File "C:/Users/justin/Python/Programs I Made/Current/Testing.py", line 29, in <module>
    sol()
  File "C:/Users/justin/Python/Programs I Made/Current/Testing.py", line 7, in sol
    search()
  File "C:/Users/justin/Python/Programs I Made/Current/Testing.py", line 13, in search
    dollars = dollars + 5
UnboundLocalError: local variable 'dollars' referenced before assignment
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Justin
  • 283
  • 2
  • 5
  • 11

2 Answers2

24

You need to add global dollars, like follows

def search():
    global dollars
    print('You gain 5 bucks')
    dollars = dollars + 5
    shop()

Everytime you want to change a global variable inside a function, you need to add this statement, you can just access the dollar variable without the global statement though,

def shop():
    global dollars
    shop = input("Enter something: ")
    if shop == 'Shortsword':
        if dollars < 4:          # Were you looking for dollars?
            print('I\'m sorry, but you don\'t have enough dollars to buy that item.')
            shop1()
        if dollars > 4:
            print('Item purchased!')
            dollars -= someNumber # Change Number here
            print('You now have ' + dollars + ' dollars.')

You also need to reduce the dollars, when you shop for something!

P.S - I hope you're using Python 3, you'll need to use raw_input instead.

Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
  • 2
    Noo, don't patch it! I was exploiting my thousand shortswords! – TerryA Jul 08 '13 at 07:58
  • @Haidro : I'd like to shop like this too. :P – Sukrit Kalra Jul 08 '13 at 07:59
  • I did what was suggested, however, now it just prints You gain 5 bucks then shows a blank. It doesn't go to shop for whatever reason. – Justin Jul 08 '13 at 08:12
  • Works fine here. Except that the variable `money` is not defined. Did you mean `dollars`? Beware that since you did not pass anything to `input`, the interpreter is waiting for input with a blank screen. Try the changed code. – Sukrit Kalra Jul 08 '13 at 08:15
  • Oh wow! I can't believe I missed that.. thank you! – Justin Jul 08 '13 at 08:18
2

You need to put global dollars, on a line on its own, inside any function where you change the value of dollars. In the code you've shown that is only in search(), although I assume you'll also want to do it inside shop() to subtract the value of the item you buy...

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895