Как вы, возможно, поняли очевидные признаки ниже, я пытаюсь создать игру, типа боевого моделирования. Очень простой для назначения класса, где мы должны создать простую игру (и я, вероятно, усложняю ее, но я хотел бы повеселиться. На данный момент у нас есть основной цикл, где, если здоровье пользователя больше нуля, что в начале (100) он переходит к первому бою, и если он преодолеет все бои с ним, все еще превышающим 100, он победит. Если он опустится ниже 100, он проиграет . Моя проблема заключается в том, что при тестировании, если здоровье будет действительно низкоуровневым, здоровье пользователя не из-за следующей ошибки я на Python 2.7, если это необходимо знать информациюUnboundLocalError: локальная переменная «user_health», на которую ссылаются перед назначением
Traceback (most recent call last):
File "a3.py", line 109, in <module>
fighting_arena()
File "a3.py", line 63, in fighting_arena
menu_loop()
File "a3.py", line 37, in menu_loop
main_loop()
File "a3.py", line 69, in main_loop
easy_fight()
File "a3.py", line 96, in easy_fight
print "You have %s health and Hagen has %s health left." % (user_health, easy_opponent_health)
UnboundLocalError: local variable 'user_health' referenced before assignment
-..
def main_loop():
global user_health
user_health = 100
while user_health > 0:
easy_fight()
end_game_victory()
else:
end_game_defeat()
def easy_fight():
easy_opponent_health = 50
easy_opponent_skills = ['1', '2', '3', '4']
print '"Your first opponent is Hagen, a germanic gladiator. He bares no armor, but he has a shield and uses a long sword. Beware of his vicious strength!"'
time.sleep(2)
print 'You enter the arena surrounded by thousands of Romans demanding blood with Hagen across the field. The fight begins!'
while easy_opponent_health > 0:
a = raw_input()
b = random.choice(easy_opponent_skills)
if a == "1" and b == "1":
print "You both slashed each other!"
user_health -= 5
easy_opponent_health -= 5
print "You have %s health and Hagen has %s health left." % (user_health, easy_opponent_health)
elif a == "1" and b == "2":
print "You slashed Hagen while he managed to get a non-lethal stab!"
user_health -= 2.5
easy_opponent_health -= 5
print "You have %s health and Hagen has %s health left." % (user_health, easy_opponent_health)
elif a == "1" and b == "3":
print "Your slash only scratched Hagen as he dodged it!"
easy_opponent_health -= 2.5
print "You have %s health and Hagen has %s health left." % (user_health, easy_opponent_health)
elif a == "1" and b == "4":
print "Your slash was blocked by Hagen!"
Исправьте углубление. – sashkello