We use cookies to enhance your experience on the site
CodeWorlds

Control Flow - Choosing a Path in the Jungle

Welcome, @name! We stand at a fork in the trails. One path leads across a river, the other through dense forest. A good explorer must make decisions based on conditions. In Python, conditional statements are used to make decisions:

if
,
elif
, and
else
.

The if Statement - If the Condition Is Met

The word

if
checks a condition. If the condition is
True
, Python executes the indented block of code. If it's
False
- it skips it.

1temperature = 35
2
3if temperature > 30:
4    print("It's hot! Stay in the shade.")
5    print("Remember to drink water.")

Notice three things: after the condition we put a colon

:
, and the code to execute is indented (usually 4 spaces). This indentation tells Python: "this belongs to the condition." A condition is an expression that returns
True
or
False
- exactly what you learned with comparison operators.

if / else - Two Paths

Often we want to do one thing when a condition is met and something else when it's not. That's what

else
("otherwise") is for.

1water_supply = 2   # liters
2
3if water_supply >= 3:
4    print("We have enough water for today.")
5else:
6    print("Low on water! We must find a source.")

Python checks the condition at

if
. If it's
True
- it runs the
if
block. If
False
- it jumps to the
else
block. Exactly one of the two paths always runs.

if / elif / else - Multiple Paths

But what if there are more than two paths? Imagine evaluating temperature: hot, pleasant, or cold. That's what

elif
is for (short for "else if" - "otherwise if"). You can add as many
elif
blocks as you need.

1temperature = 22
2
3if temperature > 30:
4    print("It's hot in the jungle!")
5elif temperature >= 15:
6    print("Pleasant temperature for marching.")
7elif temperature >= 5:
8    print("Chilly, grab a jacket.")
9else:
10    print("Cold! Light a campfire.")

Python checks conditions one by one, top to bottom. It runs the block of the first condition that is

True
and skips the rest. If no condition matches, it runs the
else
block (if one exists). That's why order matters - we check from the most specific conditions.

Combining Conditions in Decisions

Remember the logical operators

and
,
or
,
not
? They work great inside conditional statements:

1temperature = 28
2is_raining = False
3
4if temperature > 20 and not is_raining:
5    print("Perfect conditions - off on safari!")
6else:
7    print("We stay in camp.")

Practice

Write a program that evaluates whether an animal is dangerous based on its weight. Complete the missing conditions in the editor:

Summary

In this lesson you learned to make decisions in code:

  • if
    - runs code when the condition is
    True
  • else
    - runs code when the condition is
    False
  • elif
    - checks further conditions when previous ones were
    False
  • The meaning of the colon
    :
    and indentation in blocks
  • Combining conditions with
    and
    ,
    or
    ,
    not

In the next lesson Darwin will teach you loops - how to repeat actions without rewriting code!

Go to CodeWorlds