replit

Before you start this course, you must login to Replit

If you have forgotten your Replit account user name or password, ask your Coder Coach.

CodeSports

10.0 Loops & Iteration (PART A) Overview

All of the lessons in 10.0 focus on iteration, also known as while loops

Using Iteration To Validate Input

Learning Objectives/Goals

Be able to read, comprehend, trace, adapt and create Python code using selection that:

  • Uses conditional iteration (while loops)
  • Validates user input
  • Repeats whilst the user does not input the desired data.

Slide Deck

The slides to accompany this topic are here

TEACHER NOTES

Iteration

Iteration is used to loop certain lines of code to prevent the coder having to type them again and again.

There are several different ways of doing this. We are using a **conditional **iteration called a while loop. This type of loop runs while a condition (or conditions) is/are true.

A while loop starts with the command ** while **followed by the condition(s) for the loop to run.

The loop will only run while the condition is true, otherwise the program will skip the loop and carry on with the rest of the program

Instructions to be repeated go inside the loop, in python we do this by indenting the code.

Students often have trouble identifying which instructions should be inside the loop. Question them to get them to think about which steps of their algorithm the computer should repeat.

CODE EXAMPLES

The code for a while loop looks like this:

while (condition goes here):
	Instructions to be repeated are indented to put them inside the loop
	This can be as many instructions as necessary.
	They will be repeated as long as the condition is true.

This instruction is not in the loop because it isn't indented.  The loop has now ended.

TEACHER NOTES

Previously we have used selection to create error messages for incorrect input, using iteration allows us to get the program to repeat until we get the input that we want.

The plain English algorithm for this looks like:

  1. Get the input & store in a variable.
  2. Start the loop, set the condition to repeat whilst the input is not the same as the desired data.
  3. Output an error message.
  4. Get the input again, store in the same variable as before.
  5. End the loop
  6. Output a success message

The condition should be set to be true when the input is **NOT **what is acceptable.

For text input use != not equal to.

For number range use the ‘outside a range’ conditions from the selection lessons.

Students often miss the ‘get input again inside the loop’ step. If they do this, they will create an infinite loop that never ends because the variable storing the input will never be changed.

CODE EXAMPLES

Checks a password and loops until the user enters the correct one.

correctPW = "pa55w0rD"

Get the input

password = input("Enter your password")

Set the loop with condition to catch incorrect input

while correctPW ! = password:

Get input again inside the loop – this will be repeated while the condition is true

	password = input("Incorrect password, try again.")

Loop ends by not indenting the next line.

Outputs once the loop ends – when the condition is false.

print("Password accepted")

Loops while number input is not between 1 and 10

Get the input

num1 = input("Enter a number between 1 and 10")

Set the loop with condition to catch input outside the range

while num1 < 1 or num1 > 10:

Get input again inside the loop – this will be repeated while the condition is true

	num1 = input("Incorrect input, try again.")

Loop ends by not indenting the next line. Outputs once the loop ends – when the condition is false.

print("Number accepted)"

Errors & Misconceptions To Watch Out For

  • Not getting input before the loop starts
  • No colon at the end of the loop condition
  • Not getting input again inside the loop
  • Not using the same variable to store input inside the loop
  • Not indenting the code that belongs inside the loop
  • Indenting the success message to make it part of the loop

Hints