Back to 课程

Computer Science GCES EDEXCEL

0% Complete
0/0 Steps
  1. Decomposition And Abstraction Edexcel
    2 主题
  2. Algorithms Edexcel
    11 主题
  3. Truth Tables Edexcel
    3 主题
  4. Binary Edexcel
    6 主题
  5. Data Representation Edexcel
    4 主题
  6. Data Storage And Compression Edexcel
    2 主题
  7. Hardware Edexcel
    5 主题
  8. Software Edexcel
    3 主题
  9. Programming Languages Edexcel
    2 主题
  10. Networks Edexcel
    7 主题
  11. Network Security Edexcel
    2 主题
  12. Environmental Issues Edexcel
    1 主题
  13. Ethical And Legal Issues Edexcel
    3 主题
  14. Cybersecurity Edexcel
    2 主题
  15. Develop Code Edexcel
    6 主题
  16. Constructs Edexcel
    4 主题
  17. Data Types And Data Structures Edexcel
    5 主题
  18. Operators Edexcel
    1 主题
  19. Subprograms Edexcel
    2 主题
课 Progress
0% Complete

Exam code:1CP2

Programming Validation

What is validation?

  • Validation is code which is used to check that an input from a user is acceptable and that it matches the requirements of the program

  • There are 5 main categories of validation which can be carried out on fields and data types (opens in a new tab), these are:

    • Length check

    • Range check

    • Presence check

    • Pattern check

  • There can be occasions where more than one type of validation will be used on a field

  • An example of this could be a password field which could have a length, presence and type check on it

  • An effective way of programming validation can be using while loops that terminate once the user has met the criteria

input-validation

Length check

  • Checks the length of a string

  • An example is ensuring that a password is 8 or more characters in length

  • Code example

password_length = len(password)

while password_length < 8:
password = input("Enter a password which is 8 or more characters")

Range check

  • Ensures the data entered as a number falls within a particular range

  • An example is checking a user’s age has been entered and falls between the digits of 0-100

  • Code example

age = int(input("Enter your age"))

while age < 0 or age > 100:
age = int(input("Enter your age, ensure it is between 0-100"))

Presence check

  • Looks to see if any data has been entered in a field

  • An example is checking that a user has entered a name when registering for a website

  • Code example

name = input("Enter your name")

while name == "":
name = input("You must enter your name here")

Pattern check

  • Ensures that the data has been entered in the correct pattern / format

  • An example would be ensuring that an email includes the @ symbol and a full stop (.)

  • Code example

email = input("Enter your email address")

while "@" not in email or "." not in email:
email = input("Please enter a valid email address")

  • This could be extended further and made a little more complex by ensuring that the data entered into a program follows the set format

  • In the example below, the user must enter a postcode in the format of LL00 0LL where L is a letter and 0 is a number

  • This example uses a length check and string slicing covered in the previous revision note

  • Code example

valid_postcode = False

while not valid_postcode:

postcode = input("Enter a postcode (in the format LL00 0LL): ")

# Check if the length is correct

if len(postcode) != 8:

print("Invalid postcode length. Please enter again.")

continue

# Check if the format is correct

if not (postcode[:2].isalpha() and

postcode[2:4].isdigit() and

postcode[4] == ' ' and

postcode[5].isdigit() and

postcode[6:].isalpha()):

print("Invalid postcode format. Please enter again.")

continue

print("Valid postcode entered:", postcode)

valid_postcode = True

Worked Example

A car dealership uses a computer system to record details of the cars that it has for sale. Each car has a make, model, age and number of miles driven.

The car dealership only sells cars that have fewer than 15,000 miles and are 10 years old or less.

Write an algorithm that will:

  • Ask the user to enter the number of miles and the age of a car

  • Validate the input to check that only sensible values that are in the given range are entered

  • Output True if valid data has been entered or False if invalid data has been entered [4]

How to answer this question

  • When answering any algorithm question, ask yourself:

    • What inputs and outputs do I need?

    • Do I need to do any calculations or comparisons?

    • Do I need to use selection or iteration?

    • Do I need to use a function or procedure?

  • Re-read the algorithm question working through the criteria given

Programming Skill

Algorithm

Inputs

  • Miles

  • Age

Outputs

  • True or False

Calculations / Comparisons

  • Check for valid mileage

  • Check for valid age

Selection or Iteration

  • Selection is needed (If age <10 and miles < 15000)

  • Iteration is not needed

Function or Procedure

  • Not needed

Answer: i)1 mark per bullet, max 4

  • Miles and age input separately

  • Checks for valid mileage

  • Checks for valid age

  • Checks both are greater than / greater than equal to zero

  • …correctly outputs both True and False

Example Answer:

miles = int(input("enter miles driven"))
age = int(input("enter age of car"))
valid = True
if miles > 15000 or miles < 0 :
valid = False
elif age > 10 or age < 0 :
valid = False
print(valid)

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注