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 / number
5 print(answer)
6 break
7 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.

1 / 15

Step through the trace — every value is the lesson’s, not run by the page.

Step 1 of 15: Start here. while True: repeats, and nothing in the loop stops it except a break..

Exam line: Exception handling doesn't stop the error from happening. The error still occurs; the program detects it, deals with it and keeps running.
Watch out: Look at which except block runs each time: ValueError for dog, ZeroDivisionError for 0. Same program, two different errors.

Computer Science · Robust programs

"It works for me"

Priya writes a program that asks for a number. She tests it by typing 5 and it works perfectly.

Is Priya's program robust? Which idea is closest to what you think?
How sure are you?

Exam line: A program that works on the input you expected is not automatically robust. Robust code copes with the input you didn't expect.

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.

7 of 7 still to sort.

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.

Exam line: Programs need to cater for all likely inputs, and each kind of bad input needs the check that deals with it.
Watch out: Out of range and not a valid whole number are different problems. 12 is a perfectly good whole number; it's just too big.

Computer Science · Robust programs

Write the validation loop

Finish this loop so it only accepts ages from 13 to 100, including both 13 and 100. If the age is no good, it shows a message and asks again.

  1. while True:The loop keeps asking until we tell it to stop.
  2. age = int(input("Enter your age: "))This line takes the input. A word like dog would still raise a ValueError here. That's the job for try and except from earlier.
  3. missing step
Which line is step 3?

Exam line: Validation sits inside a loop that keeps asking until the input is valid. An if statement does the checking and break ends the loop.

Two safeguards, two jobs

ValidationvsException handling

A crash is when a program suddenly stops working because of an error it cannot recover from. Both safeguards are about avoiding that.

Focus

Does it stop the error happening?

Validation

Checks data against criteria before the program uses it, so invalid data can be caught early.

Exception handling

No. The error still happens. The program detects it and keeps running.

The insight

This is the difference people mix up most. Exception handling doesn't prevent an error; it deals with one that has already occurred.

When does it act?

Validation

Before the data is used.

Exception handling

When an error occurs while the code runs.

What does it look like in Python?

Validation

if statements testing the input, usually inside a while True loop with a break.

Exception handling

A try block for the risky code, and an except block for each error type to catch.

Example from this lesson

Validation

A range check for an age between 13 and 100.

Exception handling

except ValueError for dog, except ZeroDivisionError for 0.

What does it protect?

Validation

Helps prevent crashes, keeps data accurate and reliable, and improves the user experience by catching mistakes early.

Exception handling

Stops the program crashing, and lets it show a message or ask the user to try again.

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

1Robust means coping with errors or unexpected input without crashing.
2Validation checks data (empty string, minimum length, range) before it is used.
3A while True loop, an if test and a break keep asking until the input is valid.
4try and except catch an error that has happened. They don't prevent it.
5One except block per error type: ValueError for a word turned into a number, ZeroDivisionError for dividing by zero.

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?
It copes with errors or unexpected input without crashing, using checks and safeguards to make sure data is valid and errors are handled.
What is a crash?
A program suddenly stopping working because of an error it cannot recover from.
Why can't a programmer just trust the user to type what the prompt asks for?
You cannot always predict what a user will enter. Real users might type different types of data, even when the program works with the expected input.
What does an empty string check do?
It makes sure the user has entered something.
What does a minimum length check do?
It validates that the input has at least a minimum number of characters.
What does a range check do?
It confirms that a number falls within a set range, such as an age between 13 and 100.
What is validation?
Checking that data is acceptable by comparing it against a set of criteria before the program uses it.
Name the benefits of validation.
It ensures data is reasonable, helps prevent crashes, helps keep data accurate and reliable, and improves the user experience by catching mistakes early.
What does the while True loop do in a validation program?
It repeats the question again and again, so the program keeps asking until the input is valid.
What is exception handling?
Code that catches and manages errors so the program does not crash.
What do the try block and the except block each do in Python?
try holds the code that might cause an error. If an error occurs, control passes to except, which can show a message or ask the user to try again.
When does a ValueError happen?
When a string that isn't a valid number is converted to a number, such as int(input()) being given a word.
When does a ZeroDivisionError happen?
When a number is divided by zero.

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 posted

More Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

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.