Dark theme

Adjust your code so it looks like this:

    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!")
        print("You FIGHT!")
        print("SMASH!")
        time.sleep(0.5)
        print("BASH!")
        time.sleep(0.5)
        print("Oo!")
        time.sleep(0.5)
        random_number = random.randint(10)
        if player_strength > random_number:
            print("You won! Gain one player strength!")
            player_strength = player_strength + 1
        else:
            print("You lost! Lose one player strength!")
            player_strength = player_strength - 1
        if player_strength < 1:
            print("Oh no! You died!")
            sys.exit()

Have a good look at this code. You should be able to work out most of what it is doing, with the help of the following pointers:

  • time.sleep() puts the program to sleep for a set number of seconds. This code gives the impression of time passing - otherwise the code would happen in a blink of your eye.
  • random.randint() generates a random whole number (no decimal places) between zero and the number entered. Integers (or "ints") are what we call whole numbers in computing. You can change the player_strength of the enemy by changing the random number size. Bigger numbers are more likely to be bigger than the player's player_strength.
  • Note also we're used what's called snake_case to name the variable random_number; variable names can't have spaces, so we use underscores instead. You may see other ways of writing names, like camelCase, but snake_case is traditional for Python, and sticking to the traditions helps everyone to recognise a variable when they see one.
  • player_strength = player_strength + 1 increases the player_strength variable by one. It works by calculating the right side of the equation and then attaching the answer to the variable on the left. You could use a new variable, but this re-use of a variable is quite common and means it changes everywhere you might use the "player_strength" variable. You may also see this written using the shorthand player_strength += 1.
[Home > Part 2 > Next]