0% found this document useful (0 votes)
20 views

Programming KS4

This document discusses while loops and how to use them in Python programs. It includes activities to introduce iteration and while loops, modifying a number guessing game to use a while loop, and extending the number guessing game with pair programming exercises. The homework is to complete the number guessing game activities.

Uploaded by

pete555
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Programming KS4

This document discusses while loops and how to use them in Python programs. It includes activities to introduce iteration and while loops, modifying a number guessing game to use a while loop, and extending the number guessing game with pair programming exercises. The homework is to complete the number guessing game activities.

Uploaded by

pete555
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lesson 12: While loops

KS4 - Programming
Starter activity

Make a prediction (think, pair, share)

print("What’s your name?") Question Can you predict what the outcome
name = input() of running this program will be?

while name != "Hedy":


Tip: while block These statements are to be
print("Try again Hedy")
repeated.
name = input()
Tip: while condition This condition is
print(f"Hello, {name}")
checked at the beginning of each loop.

Answer This program will keep asking the


user for their name, until the name is "Hedy",
at which point it will display a greeting.
Objectives

Lesson 12: While loops


In this lesson, you will:
● Define iteration as a group of instructions that are repeatedly executed
● Modify a program to incorporate a while loop

3
Activity 1

Iteration

Programs repeat actions, checking for a terminating condition at the beginning of


each new loop.
Activity 1

Iteration

while condition
: You will need a while statement:
block of When your program needs to repeat actions, while a
statements
condition is satisfied.
Activity 1

Iteration: step-by-step execution

print("What’s your name?") State .


name = input()
name "Margaret"
while name != "Hedy": True

print("Try again Hedy") Input/output .


name = input()
What’s your name?
print(f"Hello, {name}")
User types Margaret
Try again Hedy
User types Ada
Activity 1

Iteration: step-by-step execution

print("What’s your name?") State .


name = input()
name "Ada"
while name != "Hedy": True

print("Try again Hedy") Input/output .


name = input()
What’s your name?
print(f"Hello, {name}")
User types Margaret
Try again Hedy
User types Ada
Try again Hedy
User types Hedy
Activity 1

Iteration: step-by-step execution

print("What’s your name?") State .


name = input()
name "Hedy"
while name != "Hedy": False

print("Try again Hedy") Input/output .


name = input()
What’s your name?
print(f"Hello, {name}")
User types Margaret
Try again Hedy
User types Ada
Try again Hedy
User types Hedy
Hello Hedy
Activity 1

Subtle points

print("What’s your name?") Question .


name = input() ⨯
What difference will it make if the line in red is
while name != "Hedy": removed?
print("Try again Hedy") 1A. It will make no difference.
name = input() ▹ 2B. There’s no initial value for name:

print(f"Hello, {name}") An error will occur when checking the


condition in while.
3C. There’s no initial value for name:

An error will occur when executing print.


4D. There’s no initial value for name:

The program will execute normally.


Activity 1

Subtle points

print("What’s your name?") Question .


name = input() What difference will it make if the line in red is
while name != "Hedy": removed?
print("Try again Hedy") (Assume the first name is not "Hedy".)
name = input() ⨯
1A. It will make no difference.
print(f"Hello, {name}") 2B. The value for name is never modified:
An error will occur when checking the
condition in while.
▹ 3C. The value for name is not modified:

The program will never terminate.


Activity 2

Adding iteration to a ‘guess the number’ game

number = 4
Live coding
print("Guess a number between 1 and 10")
guess = int(input())
if guess != number:
print("Incorrect")
else:
print("Correct")
Activity 3

Extending the ‘guess the number’ game

For the next activity you will be doing


some pair programming.

Remember to swap driver and


navigator roles regularly.

Use the worksheet to guide you through


the challenges.
Plenary

Parson’s Puzzle

Use the plenary worksheet to complete


the Parson’s Puzzle activity.
Plenary

Parson’s Puzzle: solution

print("Guess the word")


word = input()
while word != "Raspberry":
print("Try again...")
word = input()
print(f"Well done, the word was {word}!")
Homework

Homework: Complete the ‘guess the number’ game

Complete all aspects of the ‘Extending


the guess the number game’ worksheet,
including the explorer tasks.

Make sure that each person in the pair has


their own copy of the code.

Due: Next lesson

15
Summary

Next lesson

In this lesson, you… Next lesson, you will…

Learnt about iteration and applied a while Use selection within a while loop.
loop to your code.

16

You might also like