Input validation with a while loop

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Input validation with a while loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is the main job of an input validation loop?
A Keep asking until the input is valid
B Run the program one time and then stop
2. Which loop is best for asking again until the answer is valid?
A for i in range(3) loop
B while loop
3. Which range check makes sure an age is between 1 and 120?
A age >= 1 and age <= 120
B age >= 1 or age <= 120
4. Which loop condition keeps asking while the age is NOT in the range 1 to 120?
A while age < 1 and age > 120:
B while age < 1 or age > 120:
5. When should the error message be shown inside the loop?
A Each time the user enters invalid input
B Only after the user enters valid input

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1while age < 0 or age > 120: AAsks the user again inside the loop so they get another chance to enter a valid answer
2age = int(input("Try again: ")) Bprint("Age must be 0 to 120")
3Show an error message each time the input is invalid so the user knows what went wrong CThere is no new input inside the loop, so age never changes and the loop runs forever. Add age = int(input(...)) inside the loop.
4A range check that stays in the loop while a score is outside 1 to 10 Dwhile score < 1 or score > 10:
5while age >= 0 and age <= 120: age = int(input("Age: ")) EThe condition checks for VALID ages, so the loop runs when the input is good and stops when it is bad. It should loop while the age is invalid (age < 0 or age > 120).
6while age < 0 or age > 120: print("Invalid age") FKeeps looping while the age is outside the allowed range of 0 to 120

Definitions

while not valid:

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
age = int(input("Enter age: ")) _____ age < 0 or age > 120: age = int(input("Try again: "))
2.
age = int(input("Age: ")) while age < 0 __ age > 120: age = int(input("Age 0-120: "))
3.
while age < 0 or age > 120: _____("Invalid age. Try again.") age = int(input("Age: "))
4.
while age < 0 or age > 120: print("Age must be 0 to 120") age = int(_____("Try again: "))
5.
score = int(input("Score out of 10: ")) while score < 0 or score > __: score = int(input("Score out of 10: "))

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A age = int(input("Age: "))
B while age < 0 or age > 120:
C print("Invalid age"
D age = int(input("Age: "))
Question 2
A age = input("Age: ")
B while age < 1 or age > 120:
C print("Enter 1 to 120")
D age = int(input("Age: "))
Question 3
A mark = int(input("Mark: "))
B while mark < 0 or mark > 100:
C print("Must be 0 to 100")
D mark = int(imput("Mark: "))
Question 4
A age = int(input("Age: "))
B while age < 0 and age > 120:
C print("Age must be 0-120")
D age = int(input("Age: "))
Question 5
A num = int(input("Pick 1-10: "))
B while num < 1 or num > 10:
C print(Try again)
D num = int(input("1-10: "))

Answer Key

Input validation with a while loop
Section A: Quick Questions
Q1
A — Keep asking until the input is valid
Q2
B — while loop
Q3
A — age >= 1 and age <= 120
Q4
B — while age < 1 or age > 120:
Q5
A — Each time the user enters invalid input
Section B: Match the Pairs
Q1
while age < 0 or age > 120: → Keeps looping while the age is outside the allowed range of 0 to 120
Q2
age = int(input("Try again: ")) → Asks the user again inside the loop so they get another chance to enter a valid answer
Q3
Show an error message each time the input is invalid so the user knows what went wrong → print("Age must be 0 to 120")
Q4
A range check that stays in the loop while a score is outside 1 to 10 → while score < 1 or score > 10:
Q5
while age >= 0 and age <= 120: age = int(input("Age: ")) → The condition checks for VALID ages, so the loop runs when the input is good and stops when it is bad. It should loop while the age is invalid (age < 0 or age > 120).
Q6
while age < 0 or age > 120: print("Invalid age") → There is no new input inside the loop, so age never changes and the loop runs forever. Add age = int(input(...)) inside the loop.
Section C: Definitions
Q1
while not valid: — A loop that keeps asking for input again until the answer is valid, so bad data (like an age of -5) can't get into your program. Show an error message each time.
Section D: Fill in the Blanks
Q1
age = int(input("Enter age: ")) while age < 0 or age > 120: age = int(input("Try again: "))
Q2
age = int(input("Age: ")) while age < 0 or age > 120: age = int(input("Age 0-120: "))
Q3
while age < 0 or age > 120: print("Invalid age. Try again.") age = int(input("Age: "))
Q4
while age < 0 or age > 120: print("Age must be 0 to 120") age = int(input("Try again: "))
Q5
score = int(input("Score out of 10: ")) while score < 0 or score > 10: score = int(input("Score out of 10: "))
Section E: Spot the Error
Q1
C — The print() call is missing its closing bracket, so Python gives a syntax error. The error message line should be print("Invalid age").
Q2
A — input() gives text (a string). Comparing text with a number like 1 causes an error. Wrap it in int(): age = int(input("Age: ")).
Q3
D — The function name is spelled wrong. Python has input(), not imput(), so this causes a NameError when a bad mark is typed. It should be mark = int(input("Mark: ")).
Q4
B — No number is both below 0 and above 120, so 'and' is never true and bad ages get through. Use 'or': while age < 0 or age > 120:
Q5
C — The error message is text, so it needs quote marks inside print(). It should be print("Try again").