1 of 24

Lesson 4:

More branches

Year 8 – Intro to Python programming

2 of 24

Starter activity

Strange weather

print("Where do you live?")

location = input()

print("Weather in", location, "now?")

weather = input()

How would you extend this program to give advice to the user on how to dress depending on the weather?

Provide a rough outline of what your Python code would look like.

Give advice for when the weather is

cloudy, rainy, or snowy.

In any other case, display a generic message.

Discuss in pairs and write an answer together.

3 of 24

Objectives

  • Explore how selection can handle more than two possible branches
  • Use iteration (while statements) to allow the flow of program execution to include loops

In this lesson, you will...

4 of 24

Activity 1

Selection

if :

else:

condition

block of statements

block of statements

These versions of selection

check one condition and select one out of two branches to follow.

What if there are more than two cases in the problem?

What if there are multiple branches ?

if :

block of statements

condition

Think of the weather example:

“Give advice for when the weather is cloudy, rainy, or snowy.”

5 of 24

Activity 1

Multi-branch selection

if :

elif :

else:

condition

block of statements

block of statements

condition

block of statements

Multi-branch selection (if, elif, else)

checks successive conditions and

selects one out of multiple branches to follow.

You will need an if block with elif blocks:

when there are more than two mutually exclusive paths for your program to follow.

You can use multiple elif blocks.

The else and its block are optional.

You can nest anything inside the blocks of statements, even more if statements.

6 of 24

Activity 1

Strange weather

print("Where do you live?")

location = input()

print("Weather in", location, "now?")

weather = input()

Extend this program using elif, to give advice to the user on how to dress depending on the weather.

Give advice for when the weather is:

cloudy, rainy, or snowy.

In any other case, display a generic message.

Live coding (ncce.io/py-weather-40)

7 of 24

Activity 1

Strange weather: sample solution

if weather == "cloudy":

advice = "No sunglasses"

elif weather == "rainy":

advice = "Get an umbrella"

elif weather == "snowy":

advice = "Mittens and earmuffs"

else:

advice = "No particular advice"

print(advice)

8 of 24

Activity 1

Strange weather: executing the program

if weather == "cloudy":

advice = "No sunglasses"

elif weather == "rainy":

advice = "Get an umbrella"

elif weather == "snowy":

advice = "Mittens and earmuffs"

else:

advice = "No particular advice"

print(advice)

weather

"rainy"

State .

Get an umbrella

Output .

advice

"Get an umbrella"

False

True

9 of 24

Activity 1

Strange weather: executing the program

if weather == "cloudy":

advice = "No sunglasses"

elif weather == "rainy":

advice = "Get an umbrella"

elif weather == "snowy":

advice = "Mittens and earmuffs"

else:

advice = "No particular advice"

print(advice)

weather

"clear"

State .

No particular advice

Output .

advice

"No particular advice"

False

False

False

10 of 24

Activity 1

Retrieving the weather

from ncce.weather import description

print("Where do you live?")

location = input()

weather = description(location)

print("The weather is", weather)

This modified program retrieves the weather online, instead of asking the user.

Note The weather module and its functions are not standard Python components.

Open the modified program in your development environment (ncce.io/py-weather-42).

Run it a few times and enter different locations around the world.

11 of 24

Activity 2

People in space

You will be given a program that displays how many people are currently in space.

Note: The number is retrieved from an online service. It is not always the same.

Extend this program so that it asks the user to guess this number.

Use your worksheet.

12 of 24

Activity 2

from space import people

number = people()

print("How many people...")

guess = int(input())

if guess < number:

print("It's actually more than that.")

elif guess > number:

print("It's actually less than that.")

else:

print("That's right!")

print(number, "people in space now")

People in space: sample solution

Prompt the user to guess .

Retrieve the number of people in space .

Check the answer and provide feedback .

Display the number of people in space .

13 of 24

Activity 3

Something missing

print("What’s your name?")

name = input()

print("Hello", name)

This program greets the user by name.

What if... .

we wanted to repeat the process until a specific name is entered?

Many of our programs have sections that could be repeated

e.g. checking the weather for multiple locations, and a number guessing game with many guesses.

14 of 24

Activity 3

Iteration

When your programs repeat actions, checking for a terminating condition at the beginning of each new loop

15 of 24

Activity 4

Iteration

while :

condition

block of statements

while True:

block of statements

16 of 24

Activity 4

Iteration

while :

condition

block of statements

You will need a while statement:

when your program needs to repeat actions while a condition is satisfied.

17 of 24

Activity 4

Example: a (surreal) iterative program

Tip: while block These statements are to be repeated.

Question Can you predict what the outcome of running this program will be?

print("What’s your name?")

name = input()

while name != "Hedy":

print("Try again Hedy")

name = input()

print("Hello", name)

Answer This program will keep asking the user for their name, until the name is "Hedy", at which point it will display a greeting (ncce.io/py-you-4).

Tip: while condition This condition is checked at the beginning of each loop.

18 of 24

Activity 4

print("What’s your name?")

name = input()

while name != "Hedy":

print("Try again Hedy")

name = input()

print("Hello", name)

Iteration: step-by-step execution

name

"Margaret"

State .

Try again Hedy

Input/Output .

True

What’s your name?

User types Margaret

User types Ada

19 of 24

Activity 4

print("What’s your name?")

name = input()

while name != "Hedy":

print("Try again Hedy")

name = input()

print("Hello", name)

Iteration: step-by-step execution

name

"Ada"

State .

Try again Hedy

Input/Output .

True

What’s your name?

User types Margaret

User types Ada

Try again Hedy

User types Hedy

20 of 24

Activity 4

print("What’s your name?")

name = input()

while name != "Hedy":

print("Try again Hedy")

name = input()

print("Hello", name)

Iteration: step-by-step execution

name

"Hedy"

State .

Try again Hedy

Input/Output .

False

What’s your name?

User types Margaret

User types Ada

Try again Hedy

User types Hedy

Hello Hedy

21 of 24

Activity 4

print("What’s your name?")

name = input()

while name != "Hedy":

print("Try again Hedy")

name = input()

print("Hello", name)

Subtle points

Question .

What difference will it make if the line in red is removed?

  1. It will make no difference.
  2. There’s no initial value for name:

an error will occur when checking the condition in while.

  1. There’s no initial value for name:

an error will occur when executing print.

  1. There’s no initial value for name:

the program will execute normally.

1

2

3

4

22 of 24

Activity 4

print("What’s your name?")

name = input()

while name != "Hedy":

print("Try again Hedy")

name = input()

print("Hello", name)

Subtle points

Question .

What difference will it make if the line in red is removed?

(Assume the first name is not "Hedy".)

  1. It will make no difference.
  2. The value for name is never modified:

an error will occur when checking the condition in while.

  1. The value for name is not modified:

the program will never terminate.

1

2

3

23 of 24

Homework

Answer the questions in your homework sheet.

24 of 24

Summary

In this lesson, you...

Next lesson, you will...

Use iteration (while statements) to allow the flow of program execution to include loops

Use Boolean variables, operators, and expressions

Use variables as counters and flags

Explored how selection can handle more than two possible branches

Used iteration (while statements) to allow the flow of program execution to include loops