← VCE Software Development U3O1

Input validation with a while loop

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.

Understanding Input validation with a while loop in VCE Software Development U3O1

What it does: This print() line sits inside the loop and tells the user why their answer was rejected before re-asking. The condition is true for any score that is too low or too high, so the loop only ends when the score is from 1 to 10. A validation loop stops bad data (like an age of -5) from getting into your program by asking again. We don't know how many tries the user will need, so a while loop that repeats until the input is valid is the best fit. A range check needs both conditions to be true, so use 'and'. With 'or', even -5 or 500 would pass.

Common mistakes: The print() call is missing its closing bracket, so Python gives a syntax error. The error message line should be print("Invalid age"). input() gives text (a string). Comparing text with a number like 1 causes an error. Wrap it in int(): age = int(input("Age: ")).

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 114–124) and word search (Modules 114–141).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What is the main job of an input validation loop?
Keep asking until the input is validRun the program one time and then stop
Matches
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).
Applying
Meanings
Term
while not valid:
Meaning
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.
Practice
valid = False
Testing
Gaps
age < 0 or age > 120:
Check Answer
Spots
2while age < 0 or age > 120:
3 print("Invalid age"

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Manta Ray' badge — Graceful Coder, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

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
A validation loop stops bad data (like an age of -5) from getting into your program by asking again.
2. Which loop is best for asking again until the answer is valid?
A. for i in range(3) loop
B. while loop ✓
We don't know how many tries the user will need, so a while loop that repeats until the input is valid is the best fit.
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
A range check needs both conditions to be true, so use 'and'. With 'or', even -5 or 500 would pass.
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: ✓
An age is bad if it is too low OR too high. No number can be both below 1 and above 120, so 'and' would never run.
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
The error message tells the user what went wrong so they can fix it. It belongs inside the loop when the input is bad.

Match the Pairs

while age < 0 or age > 120: → Keeps looping while the age is outside the allowed range of 0 to 120
age = int(input("Try again: ")) → Asks the user again inside the loop so they get another chance to enter a valid answer
Show an error message each time the input is invalid so the user knows what went wrong → print("Age must be 0 to 120")
A range check that stays in the loop while a score is outside 1 to 10 → while score < 1 or score > 10:
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).
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.

Key Term

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.

Fill in the Blanks

1. age = int(input("Enter age: ")) while age < 0 or age > 120: age = int(input("Try again: "))
Hint: Type the keyword that keeps repeating the loop while the input is bad
2. age = int(input("Age: ")) while age < 0 or age > 120: age = int(input("Age 0-120: "))
Hint: Type the logical operator that catches a number that is too low OR too high
3. while age < 0 or age > 120: print("Invalid age. Try again.") age = int(input("Age: "))
Hint: Type the function that shows an error message to the user
4. while age < 0 or age > 120: print("Age must be 0 to 120") age = int(input("Try again: "))
Hint: Type the function that asks the user to enter the answer again
5. score = int(input("Score out of 10: ")) while score < 0 or score > 10: score = int(input("Score out of 10: "))
Hint: Type the highest number allowed in this range check

Spot the Error

Question 1
A age = int(input("Age: "))
B while age < 0 or age > 120:
C print("Invalid age"
D age = int(input("Age: "))
Line C — The print() call is missing its closing bracket, so Python gives a syntax error. The error message line should be print("Invalid 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: "))
Line A — input() gives text (a string). Comparing text with a number like 1 causes an error. Wrap it in int(): 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: "))
Line 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: ")).
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: "))
Line 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:
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: "))
Line C — The error message is text, so it needs quote marks inside print(). It should be print("Try again").

Student with a class code? Join your class →