Legends ofPythos
Claim your name
Python Basics

Making decisions

Lesson 7 of 13

Watch the lesson1:02 · with Sigrid
So far, your programs have run from top to bottom without stopping or choosing paths. Real software needs to react to different situations: a login check, a weather alert, or a game move. Python gives you this power with the if statement.

The if statement

age = 18
if age >= 18:
    print("You can vote.")
print("Program ends.")
Runs the indented line only if condition is True
Look closely at that structure. The if line ends with a colon (:). This tells Python: "I am about to define a block of code." The lines underneath are indented (usually four spaces). That indentation is not for looks; it defines which commands belong to the decision.
If age >= 18 is True, Python runs the print inside. If it is False, Python skips that indented block and continues with whatever comes after at the same level as if. This skipping behavior is key: code outside the indent always runs.

else: the alternative path

number = 7
if number % 2 == 0:
    print("Even")
else:
    print("Odd")
Checks if a number is divisible by two
else handles the case where the if condition was False. It must follow an if. You cannot use else on its own. In this example, we check if a number is even using the modulo operator %, which gives you the remainder of division.

elif: multiple options

temp = 5
if temp > 25:
    print("Hot")
elif temp < 10:
    print("Cold")
else:
    print("Mild")
Checks conditions in order until one is True
elif stands for "else if." It lets you check multiple options in a single chain. Python reads them top to bottom and stops as soon as it finds one that is True. else catches everything else.

Nesting: decisions inside decisions

is_adult = True
has_license = False
if is_adult:
    if has_license:
        print("Can drive")
    else:
        print("No license yet")
else:
    print("Too young to check license")
An if inside an if
Nesting means putting one if block inside another. You use this when a second condition only matters if the first was true. Here, we check for a license only because the person is already an adult.

Your turn

0 of 3 solved

Exercise 1

+30 XP
Three motion sensors report pulse counts. For each one, use if/else with the modulo operator % to decide whether the count is even or odd, storing 'Even' or 'Odd' in verdict_hall, verdict_door and verdict_shed. No shortcuts — write the check three times; in a few lessons you will learn how to stop repeating yourself.
pulses_hall = 42
pulses_door = 7
pulses_shed = 0

Run your code to check it against the tests.

Exercise 2

+30 XP
A weather station logs three temperatures. Use an if/elif/else ladder to label each one: below 0 is 'freezing', 0 up to (but not including) 20 is 'mild', and 20 or above is 'warm'. Store the labels in band_morning, band_midday and band_evening.
temp_morning = -3
temp_midday = 12
temp_evening = 26

Run your code to check it against the tests.

Exercise 3

+30 XP
A gig venue admits people who are 18 or over and have a ticket. Decide for three people, storing the answer in status_sam, status_priya and status_leo: 'admitted', 'needs ticket' (old enough, no ticket) or 'too young'. Check the age first.
age_sam = 25
has_ticket_sam = True
age_priya = 30
has_ticket_priya = False
age_leo = 15
has_ticket_leo = True

Run your code to check it against the tests.