Login

Please fill in your details to login.





lesson 3.1.4 decisions in python

Oooo - an adventure game!



image

Imagine playing a video game where your character walks in a straight line forever, ignoring every button you press. Boring, right? Today, we are going to give your code a brain. You will learn how to make your programs ask questions, compare options, and choose different paths based on the answers. This is the exact same logic Game Designers use to create branching storylines and Software Engineers use to verify passwords. Let's learn to control the flow!

Learning Outcomes
The Building Blocks (Factual Knowledge)
Recall the six comparison operators in Python (
==
,
!=
,
>
,
<
,
>=
,
<=
).
State that a condition must result in a Boolean value (True or False).
Identify the correct syntax for a decision block, specifically the colon and indentation.

The Connections and Theories (Conceptual Knowledge)
Describe how Selection allows a program to branch into different paths.
Explain the difference between the assignment operator (
=
) and the equality operator (
==
).
Compare a binary decision (
if
/
else
) with a multi-way decision (
if
/
elif
/
else
).

The Skills and Methods (Procedural Outcomes)
Apply relational operators to compare both strings and integers.
Create a branching text-adventure game using
if
,
elif
, and
else
to handle compass directions.

Digital Skills Focus: Use word processing software effectively to record predictions and manage screen layout (split-screening) for efficiency.

What is Selection?


So far, your programs have been sequential - like a domino topple, one instruction follows another in a straight line until the program stops. Selection changes this. It allows your program to reach a fork in the road and ask: "Is this condition True?" If yes, it runs a specific block of code. If no, it might skip it or run a different block. This is how computers make decisions.

The Logic of Decisions: Comparison Operators


To decide, a computer compares two values using Comparison Operators. The result is always a Boolean:
True
or
False
.

image

image
Ah, I see!

Warning: Do not confuse
=
(assignment) with
==
(comparison).


time limit
Task 1 The Logic Lab

1
Get Organised

1
Download the Logic Lab worksheet and open it from the download notification.
2
Type your name in the box.
3
Open up the Thonny IDE on your PC.
4
Organise your workspace!

2
Think before you start

Complete the 'Prediction' column in the table by typing "true" if you think the fact is true or "false" if you think the fact is false. Obvious really.

3
Here comes Thonny!

Switch to Thonny. In the Shell (bottom window), type each fact exactly as written in the first column and press Enter to see the result.

image
Click Me

If your prediction was correct, highlight the whole table row in  green  using the highlighter tool.
If your prediction was wrong, highlight the whole table row in  red  using the highlighter tool.

3
Think!

Here are some things to think about...

🤔 Strings are not the same as integers
🤔 A capital letter "H" is not the same as a lower case letter "h".
🤔 Strings have an 'order' determined by the position of the letter in the alphabet.

Outcome: I have tested how Python compares numbers and text to create Boolean logic. I have recorded my work in a Word document and used formatting tools to improve it's appearence and review my work.

Checkpoint

The Syntax of Selection


Python is strict about grammar (this is called Syntax). To write a decision, you need:

Keywords:
if
,
elif
(short for 'else if'),
else
.
The Colon (
:
): Marks the end of the condition and that the next line should be indented (Thonny will do this automatically if you press Enter after typing a colon).
Indentation: A tab space that groups the code belonging to that decision into a code block. This is referred to as scope.

1
Binary Choice (
if
/
else
)

Used when there are only two options (e.g., Pass/Fail, Correct/Incorrect). Try running the following code by pressing the ▶️, exhaustively testing the script, pressing the Enter key after each attempt at the password.

🤔 Code Explainer
Line 1: Ask the user for a password and store it the variable password.
Line 2: Check the password againts the correct password.
Line 3: Print "Access Granted." but ONLY if the password matches.
Line 4: A 'catchall' for anything other than the correct password.
Line 5: Print "Access Denied." for ANYTHING other than the correct password.

2
Multi-Way Choice (
if
/
elif
/
else
)

Used when there are more than two options (e.g., age ranges, directions etc). Python checks them in order, starting with the condition in the
if
moving onto the next condition until a condition returns
true
or the
else
is reached. Again, try running the following script using the ▶️ button, exhaustively testing a variety of different values including numbers and text. Remember to press the Enter key after typing each attempt.

🤔 Code Explainer
Line 1: Ask the user for their age, convert it to an integer and store it in the variable
age
.
Line 2: If the age is greater than or equal to 18, carry out the next code block.
Line 3: Print "You are an adult." ONLY if the condition on line 2 is true.
Line 4: Check the next condition - if age is greater than or equal to 13.
Line 5: Print "You are a teenager." ONLY if the condition on line 4 is true.
Line 6: 'Catchall'. If neither of the above conditions are true, run the next block.
Line 7: Print "You are a child.".


time limit
Task 2 The Lost Temple

1
Get organised

Open up the Python IDE, Thonny. If it's already open, create a new script by clicking the blank page icon (it will open a new tab).
Copy and paste the code below into Thonny.

print("You stand at the crossroads of the Lost Temple.")
direction = input("Which way do you go? (N/E/S/W): ")

# ------------------------------------------------
# FILL IN THE BLANKS (Replace the underscores!)
# Remember: Colons (:), Indentation, and ==
# ------------------------------------------------

if direction ____ "N":
    print("You walk North towards the mountains.")
    print("It is very cold here.")

____ direction == "E":
    print("You walk East into the forest.")
    print("You hear wolves howling.")

elif direction == "S":
    print("You walk South towards the desert.")
    print("____")

____ direction ____ "W":
    print("You walk West to the ocean.")
    print("You see a ship.")

____:
    print("That is not a valid direction!")
    print("You stand still and wait.")


Save the script as the-lost-temple.py but DON'T run it yet because the code isn't finished!

2
Fix the code

The code is broken! It has missing operators, colons, and keywords. Fill in the blanks (marked '
____
') to create a working game that handles inputs
N
(for North),
E
(for East),
S
(for South), and
W
(for West).

3
Hungry for more? Nested decisions.

Let's add an extra level of decision making to the South path.

1
Inside the
elif direction == "S":
block, add a new question...

    drink = input("Do you drink the water? (y/n) ")


...remembering to indent correctly.

2
Add a new
if
statement inside that block (indent it twice!) to check the answer...

    if drink == "y":
        print("You feel refreshed!")
    else:
        print("You collapse from thirst.")


Outcome: "I have written a Python program using elif to handle multiple outcomes and nested selection for complex logic."

Checkpoint

image
Today you have learnt that Selection allows your code to think. You can now use Relational Operators to compare values and strict Python syntax (if, elif, else, :, and indentation) to create programs that react differently to user choices.

Out of Lesson Learning

Last modified: November 21st, 2025
The Computing Café works best in landscape mode.
Rotate your device.
Dismiss Warning