我收到此错误,并且我已阅读其他帖子,但他们说放在global
前面dollars = 0
会产生语法错误,因为它不允许= 0
. 我将dollars
其用作计数器,因此我可以跟踪添加到其中的内容并在需要时将其显示回来。
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()
错误信息:
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