Python Control Flow Tutorial

An umbrella is a nice thing to have, but do you use an umbrella every time you go outside? Of course not! If it’s raining, you’ll most likely want to use it. Otherwise, you don’t need to.

Some parts of computer programs are like umbrellas. We only use them when we need them. How do we tell Python when to run what?

In this lesson, we’ll learn how to use if statements to decide whether to run certain parts of the code.

Python Control Flow 1

Run the code in the example.

Right now, the code outputs a message. But what if we change the value of name to “Luigi?”

Change the variable like so, then run the code again:

name = "Luigi" 

  • Question: Why wasn’t there any message?
  • Answer: Because we used an if statement. In this code, Python tests the variable
    name
    and only prints a message if its value is “Mario.”

Python Control Flow 2

What if we want a different message to appear if name is not “Mario”?

Run:

name = "Luigi"
if name == "Mario":
print("It'sa me, Mario!")
else:
print("I'm not Mario ;_;")

Now, the green code will only run if

name
is “Mario.” If
name
is anything else, the blue code will run. When we are are testing something, for example
name == "Mario"
, we use TWO equal signs. When we assign a value to a variable, we only use one equal sign.

Python Control Flow 3

Indentation tells Python if a line of code is inside an if statement or not.

Run:

num = 1
if num == 2:
print("That's 2 many! Get it?")
print("… I'll show myself out.")

Now, change the code so that the second print statement is flush with the if statement, like so:

Run:

num = 1
if num == 2:
print("That's 2 many! Get it?")
print("… I'll show myself out.")

Python Control Flow 4

This code runs the second print statement every time because it is outside the if statement.

You can put if statements inside other if statements.

Run:

first_name = "John"
last_name = "Baker"
if first_name == "John":
if last_name = "Lennon":
print("You are a music legend.")

Python Control Flow 5

You can also link tests together with the words

and
and
or
.
and
requires both tests to be true for the test to be true.
or
requires only one to be true. These words are called logical operators.

Run:

first_name = "John"
last_name = "Williams"
if first_name == "John":
if last_name == "Lennon" or "Williams":
print("You are a music legend.")

Run:

first_name = "John"
last_name = "Williams"
if first_name == "John" and last_name == "Williams":
print("Your music is in many movies.")

Python Control Flow 6

How much would you pay for a coffee? Anything less than $1 USD is great! 

Run:

price_usd = input("Enter USD Price: ") 
price_usd = float(price_usd)
if price_usd < 1:
print("Great!")

Between $1 and $3 USD is normal.

Add this to the above code:

if price_usd >= 1 and <= 3:
print("That's normal.")

>= means equal to or greater than.

<= means equal to or less than.

More than $3? Then…

Add this to the above code:

if price_usd > 3:
print("This coffee better be good!")

Python Control Flow 7

You can divide by any number, except zero. If you ever make a division-only calculator for some reason, it might look like this:

Run:

print("num1 / num2")
num1 = int(input("Enter num1: "))
num2 = int(input("Enter num2: "))
if num2 != 0:
print(num1 / num2)
else:
print("You can't divide by zero!")

Remember to change the strings to ints! You can do this on the same line where you create the new variable.

==
, <, >, !=, <=, and >= are called relational operators.

Python Control Flow 8

Run:

height = 89
if height >= 90 and height < 250 :
print("You can ride the ride!")
if height < 90:
print("You're too short to ride the ride.")
if height > 250:
print("... you're actually too tall for this ride."

  • Question: If height is less than 90, can it also be more than 250 at the same time?
  • Answer: No way! This means that the last test isn’t necessary for every height. Let’s make a few small changes to our program so Python doesn’t have to do more work than it needs to.

Python Control Flow 9

Run:

height = 89
if height >= 90 and height < 250 :
print("You can ride the ride!")
elif height < 90:
print("You're too short to ride the ride.")
else:
print("… you're actually too tall for this ride.")

  • Question: What changed?
  • Answer: The second
    if
    is now elif, which means “else if.” An elif test will only happen if the test before it comes back false.Also, the last if is now else. If the first two tests come back false, then height has to be 250 or more. No test needed!

Python Control Flow 10

Activity:

Let’s make a robot that plays rock, paper, scissors. It will ask the player for a number called

num
from 1 to 10, then ask if the player chooses rock, paper, or scissors. 

Based on the value of

num
, the robot will choose rock, paper, or scissors.

Your job is to decide how the robot will use

num
to choose rock, paper, or scissors.

Challenge your friends to see if they can figure out the pattern and beat the robot every time!

Example Pattern:

If

num
<= 3, choose rock.

If not, check to see if

num
<= 6. If so, choose paper.

Otherwise just choose scissors.

The pattern for the robot on the next page is:

If

num
is even (a multiple of 2) and greater than 5, choose rock.

If not, choose paper if

num
is odd and greater than 5.

Otherwise just choose scissors.

Save your code, as we’ll be using it again!

Example:

num = int(input("Enter a number between 1 and 10: "))
choice = int(input("Enter 1 for Rock, 2 for Paper, 3 for Scissors: "))
if num % 2 == 0 and num > 5:
print("Robot chooses Rock.")
if choice == 1:
print("It's a tie!")
elif choice == 2:
print("You win!")
else:
print("You lose!")
elif num % 2 != 0 and num > 5:
print("Robot chooses Paper.")
if choice == 1:
print("You lose!")
elif choice == 2:
print("It's a tie!")
else:
print("You win!")
else:
print("Robot chooses Scissors.")
if choice == 1:
print("You win!")
elif choice == 2:
print("You lose!")
else:
print("It's a tie.")

Remember that if number % x = 0, that means number is a multiple of x.