大家好,煎蛋,有这么一个问题,我正在做一个关于赌场的游戏
import random
import time
import os
balance = 555
def game():
game = input('Ваша ставка $ --')
if int(game) > int(balance):
print('Error')
game2 = random.randint(0,5)
game3 = input('Ваше число от 1 до 5 X -- ')
if game3 == game2:
print('Вы вииграли X -- ')
game = int(game) * 2
balance = int(game) + int(balance)
print(str(balance))
else:
print('Проигрыш :(')
balance = int(balance) - int(game)
print(str(balance))
while 1:
cmd = input('X -- ')
if cmd == 'casino':
game()
elif cmd == 'end':
break
但是,当我运行它时,会发生以下错误
Traceback (most recent call last):
File "e:\VSC\pyvsc\hack.py\hacktest.py", line 25, in <module>
game()
File "e:\VSC\pyvsc\hack.py\hacktest.py", line 7, in game
if int(game) > int(balance):
UnboundLocalError: local variable 'balance' referenced before assignment
变量balance
存在,但 def 没有看到它
可变余额不在游戏功能范围内
如果需要更改 balance 变量,请在函数内部添加全局 balance ,使其成为全局。