0% found this document useful (0 votes)
7 views10 pages

KS4 Programming: Data Validation Guide

The document provides an activity sheet for KS4 programming focused on data validation, including tasks for sorting user inputs and adding validation checks to ensure correct data entry. It outlines steps for implementing input validation in a number entry program and a name entry program, along with examples and testing instructions. Additionally, it includes a Parson's Puzzle activity for rearranging code to achieve proper validation functionality.

Uploaded by

kalsoomhumera9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

KS4 Programming: Data Validation Guide

The document provides an activity sheet for KS4 programming focused on data validation, including tasks for sorting user inputs and adding validation checks to ensure correct data entry. It outlines steps for implementing input validation in a number entry program and a name entry program, along with examples and testing instructions. Additionally, it includes a Parson's Puzzle activity for rearranging code to achieve proper validation functionality.

Uploaded by

kalsoomhumera9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

KS4 - Programming Activity sheet

L15 - Data validation

Sort the data entry


Task .
The following code is used for data entry:

print("Enter a number between 1 and 10:")


number = int(input())

Here is a list of some of the possible entries that the user could make:

Five Twelve
7 twelve
7.5 9
10
12
83
-5
0
Enter key pressed 3]
8] #
Sort the inputs into the correct categories below:

Correct Out of range

ValueError Empty string

Page 1 Last updated: 17-05-21


KS4 - Programming Activity sheet
L15 - Data validation

Resources are updated regularly — the latest version is available at: [Link]/tcc.

This resource is licensed under the Open Government Licence, version 3. For more information on this
licence, see [Link]/ogl.

Page 2 Last updated: 17-05-21


KS4 - Programming Activity sheet
L15 - Data validation

Adding validation checks


Introduction
This is a pair programming activity. Allocate roles of driver and navigator within your
pairs. Be sure to swap roles every five minutes.

Resources are updated regularly — the latest version is available at: [Link]/tcc.

This resource is licensed under the Open Government Licence, version 3. For more information on this
licence, see [Link]/ogl.

Page 1 Last updated: 30-05-2024


KS4 - Programming Activity sheet
L15 - Data validation

Enter a number program .

Open the start code in your development environment (IDE) or use the Trinket
([Link]/validationstartercode) link.

1 try:
2 print("Enter a number between 1 and 10:")
3 number = int(input())
4 except ValueError:
5 print("You must enter a number between 1 and 10:")
6 number = int(input())

Live coding

Your teacher will now live code with you to modify this code so that it repeats. The
navigator should help the driver stay on track as you live code with your teacher.

Adding a range check .

Note: Complete the following activity by extending the code that you started in the live
coding session.

Step 1

An if statement is required to check if the number entered is within the correct range.
Some incomplete code has been created below to assist you with this:

if and :
not_validated = False
else:
print("Number entered out of range")

Place a tick ✓ next to the instruction when you have completed it.

Page 2 Last updated: 30-05-2024


KS4 - Programming Activity sheet
L15 - Data validation

Complete the code and incorporate it within the while loop.

Tip: The if statement will need to run after an integer has been correctly entered.

Step 2
Test your code to make sure that it works as expected.

Example
Note: Use this example to check your program. Given the input you see in this sample interaction, this is
the output your program should produce.

The user is prompted to enter a Enter a number between 1 and 10:


number

The user enters a letter F

The user is reminded to enter a You must enter a number between 1 and
number between 1 and 10 10:
Enter a number between 1 and 10:
The user enters a number 12

The user is told that the number is Number entered out of range
out of range and asked to enter Enter a number between 1 and 10:
the number again

The user enters a number 7

The program ends >>>

Page 3 Last updated: 30-05-2024


KS4 - Programming Activity sheet
L15 - Data validation

Check for all likely inputs .

Step 1

Your code should now work when all likely inputs are entered. Test your code to make
sure that it doesn’t break when:

The correct data is entered.


The data entered is out of range.

The data entered is not an integer.


No data is entered (the user presses Enter without entering any input).

Enter a name program .

Step 1

Create a new file in your development environment (IDE) and enter the code
below:

1 print("Enter a name:")
2 name = input()
3 print(f"Stored name: {name}")

Step 2

The user must not be able to leave this question blank.

Create a while loop that will continue to ask for the name if the user presses
the Enter key.

Tip: If nothing is entered in name, then it will be equal to ""

Page 4 Last updated: 30-05-2024


KS4 - Programming Activity sheet
L15 - Data validation

Step 3

Test your program to make sure that the user is prompted if they press the
Enter key without entering any data.

Example
Note: Use this example to check your program. Given the input you see in this sample interaction, this is
the output your program should produce.

The user is prompted to enter a Enter a name:


name

The user presses the Enter key

The user is reminded that the Name cannot be left blank


name cannot be left blank and is Enter a name:
prompted to enter a name

The user presses the Enter key

The user is reminded that the Name cannot be left blank


name cannot be left blank and is Enter a name:
prompted to enter a name

The user enters a name


George
The name is displayed for the Stored name: George
user

Adding validation to your times table quiz .

Step 1

Find and open a copy of your completed times table quiz from Lesson 14. If
you do not have access to this, use this Trinket ([Link]/ks4-timestablequiz). It
should already have some basic validation techniques used.
Save the file under a different name.

Page 5 Last updated: 30-05-2024


KS4 - Programming Activity sheet
L15 - Data validation

Step 2

There are three opportunities for user input in this quiz.


Using the table below, make a list of the three variables that are assigned an
input.
For each variable, decide what the likely inputs might be.

Inputs Likely inputs

Step 3
Incorporate validation checks for each variable that requires user input.

Step 4
Test your program using all the likely inputs as test data.

Page 6 Last updated: 30-05-2024


KS4 - Programming Activity sheet
L15 - Data validation

Parson’s Puzzle
Introduction
This activity is a Parson’s Puzzle. A Parson's Puzzle is designed so that you can move the
code around until it provides a logical solution to the problem given.

Resources are updated regularly — the latest version is available at: [Link]/tcc.

This resource is licensed under the Open Government Licence, version 3. For more information on this
licence, see [Link]/ogl.

Page 1 Last updated: 17-05-21


KS4 - Programming Activity sheet
L15 - Data validation

Parson’s Puzzle .

Take a look at the code below. It contains all of the code needed to complete a
validation check. The program should only allow a number to be entered as input.

Your job is to rearrange the lines of code so that the program will:

● Prompt the user to enter an integer


● If the user doesn’t enter an integer, then it should remind the user to enter a
number
● It should continue to check for an integer, and remind the user until an integer is
entered

Note: You might need to add indents if they are needed

1 print("You must enter a number:")


2 try:
3 except ValueError:
4 print("Enter a number:")
5 not_validated = True
6 while not_validated:
7 number = int(input())
8 not_validated = False
9

Enter your rearranged code here:

1
2
3
4
5
6
7
8
9

Page 2 Last updated: 17-05-21

You might also like