Dark theme

We're not limited to passing in one piece of information, so can pass in the name as well:

    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 and a horrible troll!")
        fight(2, "Ogre")
        fight(3, "Troll")

Adding the following to our function:

def fight(monster_strength, monster_name):     global player_strength
    while player_strength > 0 and monster_strength > 0:
        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()

The values "2" and "Ogre" are assigned to the variables monster_strength and monster_name in the order they read, left to right.

Give this a go!

There's one last thing we need to know about functions now, and that's how to get information back out of them.

[Home > Part 3 > Next]