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: ")).
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
while not valid:Fill in the Blanks
Spot the Error
Student with a class code? Join your class →