Copy this title into a new OneNote document:
CT9 – T10 AU – While True Loops
Complete this table and copy it into the OneNote document:
| ⏱️ Do It Now: |
| Answer the following questions (1) Give the technical (proper) name for a while loop (2) Describe how we can identify a loop in a flow chart (3) Explain why looping is useful for activities such as checking a password is correct (4) Explain how a while loop differs from a conditional sequence (IF / ELSE) |
🎯 Objectives:
(1) We will be able to create programs that use a while True loop
(2) We will be able to use while True loops to carry out a range of string validation
🔎 Investigation – While True Loops
The two code boxes below contain conditional loops.
The first contains a while loop that tests the value of a pre-assigned variable (a variable that has already been given a value). You worked with loops like this in your last coding lesson.
The second contains a while True loop. This works slightly differently, and is what we will focus on during today’s lesson.
(1) Copy each and run it in a code viewer of your choice
(2) We will discuss the differences as a class
(3) You will add both sections of code to OneNote – highlighting the differences and explaining what certain commands do.
# Example 1 - while loop that tests a pre-assigned variable
#------------------------------ Global Variables -------------------------------
planet = "earth" # Initialising the variable planet
userGuess = "" # Initialising the variable userGuess
#-------------------------------- Main Program ---------------------------------
userGuess = input("Enter the name of the planet you live on: ")
while userGuess != planet:
print("Unfortunately that was incorrect")
userGuess = input("Please re-enter the name of the planet you live on: ")
print(f"Correct, you live on {userGuess}")
# Example 2 - while True loop
#------------------------------ Global Variables -------------------------------
planet = "earth" # Initialising the variable planet
userGuess = "" # Initialising the variable userGuess
#-------------------------------- Main Program ---------------------------------
while True:
userGuess = input("Enter the name of the planet you live on: ")
if userGuess == planet:
print(f"Correct, you live on {userGuess}")
break
else:
print(f"Incorrect, you don't live on {userGuess}")
print("Please try again")
⌨️ Activity 2a – Pin Number
The program below allows somebody to enter their pin at the bank
(1) Run the program and try to work out how it can be done with a while True loop
The pin is 1234
(2) Code the program in Python and then copy your evidence over to OneNote
(3) Annotate the code in OneNote to explain how it works
There is no need to make a flowchart in this activity
⌨️ Activity 2b – Extension
The program below only allows three attempts to get the pin correct.
The program either grants access or denies access.
(1) Copy your code from Activity 2a into a new file (called Activity 2b)
(2) Ammend the code so it behaves like the program below
There is no need to make a flowchart in this activity
✍️ Plenary – Reflecting on While True
In OneNote
Explain the difference between using a While True loop and using a while loop that tests the value of a pre-assigned variable.
When could each be an advantage?
When could each be a disadvantage?