← GCSE Computer Science · Edexcel · GCSE Computer Science revision
⭐ 0
Pearson Edexcel GCSE Computer Science (1CP2) · Topic 6 (6.1, 6.7) Problem solving with programming
Mini-Lesson

Problem solving with programming

2 missions. Guess, learn, then check. Your place is saved.

  1. Mission 1You typed 41, not 14, for your age. Why did the form accept it?
  2. Mission 2It passed every test. So why did it fail for an 18-year-old?
Mission 1

You typed 41, not 14, for your age. Why did the form accept it?

Make a guess: You typed 41, not 14, for your age. Why did the form accept it?

Programming · validation and authentication

What validation does

ValidationAgeValidation: range 0 to 120,presence41accepted150out of rangeask againnothing enteredask again
  • Validation is an automatic check that input is sensible and follows rules before the program uses it.
  • Invalid data is rejected, and the user is told why and asked again.
  • It stops bad data causing crashes or wrong results later.
  • Validation cannot tell whether data is correct: a mistyped value that still follows the rules gets through.
Programming · validation and authentication

Types of validation check

CheckWhat it testsExample
Range checka number is between limitsage 0 to 120
Length checknumber of characterspassword at least 8
Presence checksomething was entered (not empty)surname not blank
  • Pattern check: the data matches a set pattern, e.g. two letters then four digits.
Quick check
?A field for a 4-digit PIN accepted 12. Which check would have rejected it?
Programming · validation and authentication

Writing a validation routine

  • Ask for input, test it, and keep asking until it passes.
  • A loop does this: the user cannot move on with invalid data.
  • This WHILE loop is a range check for 1 to 10.
Python
score = int(input("Score 1 to 10: "))
while score < 1 or score > 10:
    score = int(input("Must be 1 to 10: "))
Calculate
?The user types three names in this order: an empty string, Al, then Amir. How many times is "Too short" output?
Python
name = input("Name: ")
while len(name) < 3:
    print("Too short")
    name = input("Name: ")
print("Hello " + name)
Programming · validation and authentication

Authentication

AuthenticationYesNoStartInput usernameInput passwordmatches storeddetails?Output "Accessgranted"Output "Accessdenied"Stop
  • Authentication confirms the identity of a user before they get access, e.g. with a user ID and password.
  • A lookup searches the stored list of IDs and passwords for a matching pair.
  • Robust software has few weaknesses; audit trails (logs of who did what) and code reviews (others check the code) help find vulnerabilities.
Python
users = [["sam01", "Tr33house"], ["ali22", "Bl0ckH3ad"]]
userID = input("User ID: ")
password = input("Password: ")
found = False
for user in users:
    if user[0] == userID and user[1] == password:
        found = True
if found:
    print("Access granted")
else:
    print("Access denied")
Quick check
?A programmer types OR instead of AND in the authentication condition. Which login is now wrongly granted access?
Mission check

?You typed 41, not 14, for your age. Why did the form accept it?
Mission 2

It passed every test. So why did it fail for an 18-year-old?

Make a guess: It passed every test. So why did it fail for an 18-year-old?

Programming · testing

Why test?

  • Testing checks that a program works as intended and meets its requirements.
  • It finds errors so they can be fixed before users meet them.
  • A program can run without crashing and still give wrong results.
  • Passing a few easy tests proves little: choose test data that could expose errors.
Programming · testing

Normal, edge and wrong data

Test datavalid: 1 to 10−1234678912015101115normalerroneousboundaryboundarytenerroneous

For a program that accepts whole numbers from 1 to 10:

  • Normal data: sensible values that should be accepted, e.g. 5.
  • Values at the edges of the range, 1 and 10, and just outside them, 0 and 11.
  • Data that should be rejected, e.g. ten or 15.
Calculate
?The rule is "adult ticket for 18 and over". Tests with ages 10, 30 and 50 all gave the right ticket. Which single age would show the error?
Python
age = int(input("Age: "))
if age > 18:
    print("Adult ticket")
else:
    print("Child ticket")
Programming · testing

Types of error

  • A syntax error breaks the rules of the language, e.g. a missing bracket. The program cannot be translated or run.
  • A logic error lets the program run, but it gives the wrong or unexpected result, e.g. > where ≥ was needed.
  • A runtime error happens while the program runs and makes it crash, e.g. dividing by zero or using an index past the end of an array.
  • Testing with good test data is how you find logic errors.
Quick check
?A program should output the average of three marks, 60, 70 and 80. It runs, but outputs 156.67 because the code says a + b + c / 3. What kind of error is this?
Programming · testing

Code that is easy to maintain

  • Other people (and you, later) must be able to read and change code. This is maintainability.
  • Meaningful identifiers: totalScore, not t.
  • Comments explain what each part of the code does.
  • Indentation shows which lines belong inside loops, selection and subprograms.
  • White space and a clear layout split code into readable blocks.
  • Subprograms break code into named, reusable parts.
Mission check

?It passed every test. So why did it fail for an 18-year-old?
Recap

Big ideas

  • Validation automatically checks input is sensible, e.g. range, length and presence checks, and rejects data that breaks the rules; it cannot check data is correct.
  • Authentication confirms who a user is, e.g. by matching a user ID and password.
  • Choose test data of every kind: normal, at the edges of the range, and data that should be rejected (values the program should accept, values at the limits and values it should reject).
  • Syntax errors stop a program running; logic errors give wrong results; runtime errors crash it while it runs.
  • Meaningful names, comments, indentation and subprograms make code easy to maintain.
🏆

Mini-lesson complete!

⭐⭐⭐

You've worked through Problem solving with programming for Pearson Edexcel GCSE Computer Science (1CP2). 🎉

  1. Mission 1You typed 41, not 14, for your age. Why did the form accept it?
  2. Mission 2It passed every test. So why did it fail for an 18-year-old?

Your stars: 0 / 0

Next: test yourself in the Verify stage.

📣 Smashed it? Share your score

Challenge a mate to beat your stars, or show a parent how you got on.

→ Back to GCSE Computer Science · Edexcel

Specification points and sources for this lesson are listed at the bottom of the page.

Sources (2)

The specification points and sources behind this lesson.

  1. 1 Pearson Edexcel 1CP2 spec, 3.2.3, 6.4.3, 6.4.4; PLS v4
  2. 2 Pearson Edexcel 1CP2 spec, 1.2.5, 1.2.7, 6.1.4–6.1.6; PLS v4