So I'm currently learning to code in python and I wrote this basic text game. The idea is that you fight a boss and you have 66% chance of winning the fight, but if you lose (33% chance) you lose a life and if you have 0 lives you lose the game. The problem is with the lives variable. It's first set to 3 and the program is supposed to subtract 1 live from the player after each loss and do a sys.exit when lives variable is 0. However after the first loss the program crashes and gives this error:
Traceback (most recent call last):
File "file path", line 24, in <module>
boss()
File "file path", line 21, in boss
lives = lives - 1
UnboundLocalError: local variable 'lives' referenced before assignment
here is my code:
import random, sys
lives = 3
#title screen
print('SUPER NINJA WARRIORS PLANET')
print('press A to start')
menu = input()
if menu == 'A' or 'a':
pass
else:
print('press A to start')
def boss():
print('BOSS')
print('press X to attack')
attack = input()
number = random.randint(1, 3)
if number == 1:
print('Boss defeated!')
elif number == 2:
print('Boss defeated!!')
elif number == 3:
print('Boss defeated You!')
lives = lives - 1
while True:
boss()
if lives == 0:
sys.exit
elif lives != 0:
continue