Dark theme

So far, we've passed data into our function using the area inside the parentheses. However, we can also get data out of functions. Say, for example, we want to get out of our app how long the fight took.

Adjust your function to look like this:

def fight(monster_strength, monster_name):
    global player_strength
    count = 0
    while player_strength > 0 and monster_strength > 0:
        count = count + 1
        print("You FIGHT the", monster_name)
        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()
    return count

Here we send the "count" value, which counts the number of rounds, back to where the function was called from if the loops run to completion (i.e. the player doesn't die).

[Home > Part 3 > Next]