GCSE · Computer Science · Edexcel · Spec 1CP2
Robust software development
You ask for a number. Someone types dog. What your program does next is the whole lesson.
Computer Science · Robust programs
What happens when someone types dog?
This program asks for a number and divides 10 by it. Step through three attempts: dog, 0, then 4.
Before you click Next: which part of the program will cope with dog, and which with 0? Step through and see.
1 while True:2 try:3 number = int(input("Enter a number: "))4 answer = 10 / number5 print(answer)6 break7 except ValueError:8 print("Whole numbers only. Try again.")9 except ZeroDivisionError:10 print("Cannot divide by zero. Try again.")
Variables
Output
Step 1: Start here. while True: repeats, and nothing in the loop stops it except a break.
Step through the trace — every value is the lesson’s, not run by the page.
Computer Science · Robust programs
Sort every input the prompt might get
Pick an input, then pick where it belongs. Use the Sort by switch to ask the same question a second way.
Which category does each input fall into?
Still to sort
Correct (0)
A whole number from 1 to 10.
Where the line is: Correct means a whole number inside the range, not just any number.
Out of range (0)
A whole number, but outside 1 to 10.
Where the line is: Out of range is about the size of the number. It can be read as a whole number just fine.
Not a valid whole number (0)
Can't be read as a whole number at all.
Where the line is: This one is about what was typed, not how big it is.
Empty string (0)
Enter pressed with nothing typed.
Where the line is: Nothing typed is not the number 0. It is an empty string.
The prompt says: enter a number between 1 and 10. First sort what people might type. Then switch the rule and sort the same inputs by what deals with them.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Ask for a number, get the word dog. Does your program survive?
What you need to know
- A robust program copes with errors or unexpected input without crashing.
- A crash is when a program suddenly stops working because of an error it can't recover from.
- You can't predict what a user will type: dog instead of a number, or Enter pressed by mistake.
Have a goYour friend tested their program with 5 and it worked, so they call it robust. What did that test never try?
Unexpected input, like a word or an empty Enter.
Working on expected input proves little. You can't predict what users will enter, so robust code has to cope with the unexpected too.
- Plan for every likely input: correct, out of range, not a valid whole number, or an empty string.
- Know three checks: empty string (something was entered), minimum length (enough characters) and range (a number inside set limits).
- Validation checks data against a set of criteria before the program uses it, which helps prevent crashes.
- Validation usually sits in a while True loop: if statements test the input, and break ends the loop once it's valid.
Have a goFinish it: in a validation loop, the ______ statement ends the loop once the input is valid.
break
while True keeps repeating, so break is what ends it once the input is valid.
- Exception handling catches errors so the program doesn't crash: risky code goes in try, and except deals with the error.
- Exception handling doesn't prevent the error. It lets the program detect it and keep running.
Have a goA classmate says: "I've added try and except, so now ValueErrors can't happen in my program." What do you tell them?
The ValueError still happens; the program just catches it and carries on.
Exception handling lets the program detect an error and continue instead of crashing. It doesn't stop the error occurring.
- Catch specific errors with one except block each: ValueError for a word turned into a number, ZeroDivisionError for dividing by zero.
The big picture
A robust program copes with unexpected input without crashing. Validation checks data before the program uses it, usually inside a while True loop. Exception handling (try and except) catches errors that do happen, so the program keeps running.
Key points
Worked example
Problem
A program must keep asking for a name until the user types something. Write the validation loop.
⚠ Watch out
Thinking try and except stop the error from happening. The error still occurs; except just catches it so the program carries on.
Memory hook
Look before you use it (validation). Catch it when it goes wrong (try and except).
Check yourself
A program divides 100 by the number typed in. Which error does 0 cause, which does dog cause, and what happens next?
Flashcards
(13)What makes a program robust?
What is a crash?
Why can't a programmer just trust the user to type what the prompt asks for?
What does an empty string check do?
What does a minimum length check do?
What does a range check do?
What is validation?
Name the benefits of validation.
What does the while True loop do in a validation program?
What is exception handling?
What do the try block and the except block each do in Python?
When does a ValueError happen?
When does a ZeroDivisionError happen?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Edexcel GCSE Computer Science topics
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 21 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.