GCSE · Computer Science · Edexcel · Spec 1CP2
Validation – length check
You type a new password, hit Enter, and get 'Too short'. Again. And again. Behind that is a tiny loop that refuses to let go.
Computer Science · Algorithms
Trace table — dry run the code
A user tries three passwords. Step through and watch len(password) decide, every time, whether the program asks again or saves.
Ready when you are — step through one line at a time.
Predict, then check
Same loop as above: while len(password) < 6.
Mo types secret as the first password. That's exactly six characters. What does the program do?
Pick the right check
Three checks, three different jobs
Which validation check does each situation need?
Still to sort
Empty string check (0)
Has anything been entered at all?
Where the line is: It only asks whether there is any input. It doesn't count how many characters there are.
Minimum length check (0)
Are there enough characters?
Where the line is: It counts characters with len(). It doesn't look at the value of a number.
Range check (0)
Is the number inside a set range?
Where the line is: It is for numerical input: is the number between the lowest and highest allowed?
Each situation needs one of the three validation checks. Which one?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
A loop that won't let go until the password is long enough.
What you need to know
- Validation means checking data is acceptable by comparing it against a set of criteria before the program uses it.
Have a goYour mate Jas says: 'My program validates everyone's age, so every age it stores is definitely right.' Spot the flaw before you look.
Validation only shows the data is acceptable against the criteria, not that it's right.
A check compares data with set criteria. Data can meet the criteria and still be wrong, so 'definitely right' claims more than validation can promise.
- It keeps data reasonable, accurate and reliable, helps prevent crashes, and catches mistakes early, so the program is nicer to use.
- A crash is when a program suddenly stops working because of an error it cannot recover from.
- A minimum length check makes sure input meets a minimum number of characters.
- len() counts characters, so len(password) tells the program how long the password is.
Have a goQuick one: what does len("pizza") give?
5
len() counts characters: p, i, z, z, a. It counts them; it doesn't care what the word means.
- while len(password) < 6: runs again and asks for a new password whenever the password is too short.
Have a goA user's first try is rainbow. Does while len(password) < 6: ask them for a new password?
No. len("rainbow") is 7, and 7 < 6 is False.
The loop only runs while the condition is True. Seven characters is not less than six, so the loop doesn't run at all.
- When len(password) < 6 turns False, the loop ends and the message that the password is saved is displayed.
- Another pattern: while True repeats the question, an if statement tests the input, and break ends the loop once it's valid.
- Without validation programmed in, the program runs with invalid data and may crash.
- Three checks that help make code more robust: an empty string check, a minimum length check, and a range check for numbers.
Have a goZara proudly writes a minimum length check to make sure a year group typed in is between 7 and 11. Which check did she actually need?
A range check.
A year group is a number that must fall in a set range. A minimum length check counts characters, so it can't tell 7 from 12.
The big picture
Validation checks that data is acceptable, against a set of criteria, before a program uses it. A minimum length check uses len() inside a loop: while len(password) < 6 the program asks again, and once the password is long enough the loop ends and it is saved. A while True loop with an if and a break does the same job. Empty string, minimum length and range checks each help make code more robust.
Key points
Worked example
Problem
Write a minimum length check for a new username. It must have at least 3 characters. Keep asking until it does, then display "Username saved".
⚠ Watch out
Using a single if statement with no loop. The program says the password is too short, then carries straight on with it anyway. Validation usually sits inside a loop that keeps asking until the input is valid.
Memory hook
Too short? Ask again. Long enough? Let it go. The loop only lets go when len() says so.
Check yourself
A user's first password is hello. With while len(password) < 6, what is len(password), is the condition True or False, and what happens next? (5, True, it asks again.)
Flashcards
(11)What is validation?
Give two reasons programs use validation.
What is a crash?
What does a minimum length check do?
Which Python function gives the number of characters in a string?
In while len(password) < 6:, when does the loop end?
In a while True validation loop, what ends the loop?
What is often used inside a validation loop to test the input?
What happens if a program has no validation programmed in?
What does a range check confirm?
Name the three validation checks that help make code more robust.
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
- Programming arithmetic operators
- Programming logical operators
- Programming relational operators
- Reading, writing, analysing and refining programs
- Single entry/exit code blocks
- String manipulation
- Structural components of programs
- Structured data types
- User input handling
- Validation – presence check
- Validation – range check
- Variables and constants in programs
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 3 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.