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.

1password = input("Enter a new password: ")
2while len(password) < 6:
3 password = input("Too short. Enter a new password: ")
4print("Password saved")
password
len(password)
len(password) < 6
Output
Press Start to run the first line.
·

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?

Your turn

The other way to write it: while True and break

Finish this version of the password check. It should keep asking until the password has six or more characters, then say it's saved.

  1. while True:A loop that would repeat forever, unless something breaks out of it.
  2. password = input("Enter a new password: ")The question sits inside the loop, so it gets asked again on every pass.
  3. missing step
Which line is step 3?

What does validation promise?

Your password just passed the check. So what do you actually know?

A program runs a minimum length check on a new password, and the password gets through.

Which is closest to what you think validation does?
How sure are you?

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?

6 of 6 still to sort.

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

1Validation checks data is acceptable against criteria before the program uses it. Acceptable is not the same as correct.
2A minimum length check uses len(): while len(password) < 6, the program keeps asking for a new password.
3The loop ends when the condition is False, then the password is saved.
4while True + if + break is the other way to keep asking until the input is valid.
5Empty string check, minimum length check, range check: pick the one that matches the input.

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?
Checking that data is acceptable by comparing it against a set of criteria before the program uses it.
Give two reasons programs use validation.
Any two: it ensures data is reasonable; helps prevent crashes; helps keep data accurate and reliable; improves the user experience by catching mistakes early.
What is a crash?
When a program suddenly stops working because of an error it cannot recover from.
What does a minimum length check do?
It checks that input meets a minimum character requirement.
Which Python function gives the number of characters in a string?
len(), for example len(password).
In while len(password) < 6:, when does the loop end?
When the user enters a password of six characters or more. Then the message that the password is saved is displayed.
In a while True validation loop, what ends the loop?
A break statement, once valid input is given.
What is often used inside a validation loop to test the input?
An if statement.
What happens if a program has no validation programmed in?
It runs with invalid data and may crash.
What does a range check confirm?
That numerical input falls within a set range.
Name the three validation checks that help make code more robust.
An empty string check, a minimum length check and a range check.

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 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.