Skip to content

4 – More Branches

🎯 Learning Objectives

  • Use multi-branch selection (if, elif, else statements) to control the flow of program execution
  • Describe how iteration (while statements) controls the flow of program execution

💬 Key Vocabulary

  • Multi-branch selection
  • relational operators
  • conditions
  • iteration
  • logical (or Boolean) expressions
  • execution

📝 Starter Activity – Strange Weather

  • 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 your answer in the starter worksheet.

📖 Multi-branch selection in Python

  • 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 ?

Think of the weather example:
“Give advice for when the weather is cloudy, rainy, or snowy.”

  • 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.

📝 Level up – People in Space

  • Download this lesson’s worksheet to your Home Drive.
  • Follow the instructions to complete the Level 1/2 Task – People in Space task by copying the code below into Thonny.
  • If you manage to complete the Level 1/2 task move onto the Level 3 – Temperature task below.

Level 1/2 – People in Space

number = 12
print("There are", number, "people in space now.")

Level 3 – Temperature

📖 Something Missing?

  • 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.

📖 Iteration

  • When your programs repeat actions, checking for a terminating condition at the beginning of each new loop this is called a while loop.
  • You will need a while statement: when your program needs to repeat actions while a condition is satisfied.

In this lesson, you…

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

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

🏅 Level up

🥇 Level 1

  • Mostly complete Level 1/2 – People in Space but your code does not meet all the requirements or there is an error and upload a screenshot of your code to Teams.

🥈 Level 2

  • Complete Level 1/2 – People in Space and your code meets all the requirements then upload a screenshot of your code to Teams.

🥉 Level 3

  • Complete Level 3 – Temperature on the worksheet and upload a screenshot of your code to Teams.