Copy this title into a new OneNote document:
CT8 – Y10 AU – Introducing Conditional Loops
Complete this table and copy it into the OneNote document:
| ⏱️ Do It Now: |
| Answer the following questions (1) Identify the key term we use to describe a conditional statement within another. (2) Outline what is meant by a branching conditional sequence. (3) Explain the difference between initialisation and assignment. Extension Question ————————————————————————- (4) Explain how we could test form more than one correct answer, without using a branching conditional sequence. |
🎯 Objectives:
(1) We will be able to identify a conditional looping sequence
(2) We will be able to create a conditional looping sequence
(3) We will be able to represent a conditional looping sequence with a flowchart
🔎 Investigation / Class Discussion
Analyse the code below:
(1) What do you think it does?
(2) Can the user influence alternative outcomes?
# Activity 1 Code
#------------------------------ Global Variables -------------------------------
guessNumber = 9 # Initialising a variable that stores the number the user must guess
userGuess = 0 # Initialising an empty variable to store the user's response
#-------------------------------- Main Program ---------------------------------
# Outputting a welcome message to the user
print("Welcome to the amazing number guess program...")
# Assigning an input (cast to integer) from the user to userGuess
userGuess = int(input("Guess the number I am thinking of between 1 - 10: "))
# Conditional loop requiring the user to keep trying until they correctly guess the number
while userGuess != guessNumber:
print("Unfortunately that is incorrect")
userGuess = int(input("Please guess again: "))
# Outputting a positive message to the user once they guess correctly
print("Correct... Well done!")
✍️ Representing as a flowchart
(1) Copy the code above into OneNote
(2) Work with your teacher on representing it as a flowchart
⌨️ Activity 2a – Password Program
(1) Run the following program:
The correct username is user123
The correct password is password123
Enter the wrong information a few times to see what the program does
(2) Create a flowchart that represents the program
(3) Write the program that follows the flowchart
(4) Ensure the flowchart and your code are added to OneNote
⌨️ Activity 2b – Extending your program
(1) Using a boolean operator (AND or OR)
Use only a single conditional loop (while loop) to check that both the username and password are correct
(2) You don’t need to create a flow chart for this additional task
(3) Make sure you evidence the code in OneNote
⌨️ Activity 3 – Extension Activities
(1) At the bottom of this task are examples of different checks that can be carried out on a user input.
Have a look through them.
(2) Use a conditional loop to carry out at least one of these checks in a real-world context e.g. if it asks the user to enter their age, it checks to see they have actually entered a number.
(3) Ensure you evidence any annotated code in OneNote
Examples of different checks that can be carried out:
# Examples of different validation checks in conditional sequences
userInput = input("Enter a string: ")
# Length check
if len(userInput) >= 5:
print("✓ String is at least 5 characters long")
else:
print("✗ String is less than 5 characters long")
# Alphabetic check
if userInput.isalpha():
print("✓ String contains only letters")
else:
print("✗ String contains characters other than letters")
# Numeric check
if userInput.isnumeric():
print("✓ String contains only numbers")
else:
print("✗ String contains characters other than numbers")⌨️ Plenary – Lesson Summary
Write a brief overview of what you have learned this lesson:
(1) What a conditional loop looks like, and the key terms used
(2) How it is represented in a flow chart
(3) What you used a conditional loop for