At the moment our function only works for one fight. Change it to look like this (note the starting parentheses, and that we've changed "ogre_strength" to "monster_strength"):
def fight(monster_strength):
global player_strength
while player_strength > 0 and monster_strength > 0:
print("You FIGHT!")
print("SMASH!")
time.sleep(500)
print("BASH!")
time.sleep(500)
print("Oo!")
time.sleep(500)
random_number = random.randint(10)
if player_strength > random_number:
print("You won! Gain one strength!")
player_strength = player_strength + 1
monster_strength = monster_strength - 1
else:
print("You lost! Lose one strength!")
player_strength = player_strength - 1
monster_strength = monster_strength + 1
if player_strength < 1:
print("Oh no! You died!")
sys.exit()
Then change the main code to:
decision = input("You come to a junction. Turn left or right [L,R]: ")
if decision == "L" or decision == "l":
print("You turn left and see a terrible ogre!")
ogre_strength = 2
fight(ogre_strength)
What we're doing here is passing in ogre_strength
so that the function can use it. Inside the
function, the number in ogre_strength
is copied into monster_strength
and the function
works with this. But why do this?