6 - Programming
6 - Programming
net
6 – Programming
Specification Coverage
This chapter covers all the specification content from topic 6 except the following
which are covered in chapter 1:
• 6.1.1 decomposition and abstraction
• 6.1.6 efficiency of algorithms
It is not necessary to teach this chapter in order. Use it as and when needed while
teaching students how to code. Centres are strongly advised to teach this
chapter before or alongside elements of chapter 1.
Python Files
Where Python has been used in examples, there will be two icons which are both
buttons that link to the original Python code. One icon is for the Python code in
Repl.it and the other is for the Python (.py) file:
Some activities will also include these buttons. Not all activities include the buttons
as running the Python code for those activities would make answering the question
too easy. There are several cases where there is a Python file framework for
answering tasks in an activity instead of an activity worksheet. Where answers are
provided using Python, many of the answer documents (both activity answers and
exam style question answers) will include links to Python files that include answers in
Python code, and often a step-by-step run through of the code. The icons used
for answers are:
These answer files can be used by the teacher to explain the answers to students
after they have completed the tasks, or by students once they have completed
the tasks. There are several cases where there is a Python file for answers to an
activity but not a separate Word document for the answers to an activity.
© paullong.net 2020 Page 1 of 154 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 6 - Published by paullong.net
PRIMM
Flowcharts
Where flowcharts have been used there will be an icon which will open
the file created in draw.io
Visualising Variables
Use the trace variables visualisation tool at http://tiny.cc/tracevariables
to watch what happens to the value of the variables as each line is
executed. You can input any code into the tool. If inputs are required,
then add these, including if enter needs to be input.
Contents
Global variables can be used throughout the whole programme, but local
variables can only be used within a subroutine.
myText = ""
myInteger = 0
myFloat = 0.0
myBoolean = False
myArray = []
Constant declaration
Constants are values which cannot change while a program is running. There are
two types of constants: literal constants and named constants. Like variables,
constants also reserve an area in memory to store their contents.
Literal constants are when you use a constant value within the programming
code.
“Hello World” and 5 are both values that are used ‘literally’ within the
programming code.
Named constants need to be declared. These are usually values that mean
something and do not need to change.
DAYS_IN_YEAR = 365
PI = 3.142
GRAVITY = 9.8
MAX_COUNT = 100
Assignment
The whole idea of having a variable is that it can change throughout
the program. When a value is assigned to a variable, the location of
that variable in memory stores the value. =
Scratch Link – set variables
This is an example of assigning the value 5 to the variable score:
These are examples of values being assigned to be used as if they were constants
by using the capital letters convention:
CLASS_SIZE = 28
PI = 3.14159
Activity – assignment
Write Python code to:
1) Assign the value 11 to the variable year_group.
2) Assign the Boolean value false to the variable game_over.
3) Assign the text value BLUE to the variable colour.
4) Increase the number_rooms variable by 4.
5) Assign the multiplication of variables class_size and number_rooms to
total_children.
6) Assign the value 7 to the constant days_in_week.
7) Assign the value 24 to the constant hours_in_day.
© paullong.net 2020 Page 7 of 154 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 6 - Published by paullong.net
b = 20 c=a+b p = qr s = 5t
surname = "Gates"
print(surname)
surname = "Gates"
forename = "Bill"
print(forename + " " + surname + " is good at programming")
If values are different data types, you could pass them as separate parameters:
You will find out more about how to specify datatype in the next section.
what do you think will be displayed to the screen if the user inputs 20
followed by 50?
Write code to ask the user for their forename, surname and the number of brothers
and sisters they have. Then display the full name and how many brothers and
sisters. For example, if Henry Wydale has 2 brothers and 0 sisters then the output
would be:
Questions – follow me
1) Assign the value of the speed of light (299,792,458 m/s) to a constant integer
using Python. [1]
2) Data types
The computer needs to know how to handle data. Different types of data are
stored in different ways.
Python does not have a character data type. Instead, it just has strings as all single
character values can also be stored as a string.
Note: Edexcel will use the term character to mean the same as string in an exam.
Integer
This is a number data type. Integers are whole numbers. There are never any
decimal places or fractions in an integer.
This is the Python code to receive the year as input from the keyboard.
The int() function converts the string input into an integer input.
Notice how the distance above does NOT include “m” for metres. This is because
the character “m” is not part of an integer value and units should be displayed
separately.
• it starts with a zero – a computer would not store the leading zeros if it was
represented as a numeric data type
• it includes spaces, or in some cases brackets or hyphens: (0121)-350-010
• it is not a number that will have calculations performed on it
what do you think will happen if the user inputs 0121 350 0010?
what do you think the output will be if the user inputs 01213500010?
Real
Real numbers include decimal places. Sometimes they are also sometimes called
float or double. In Python, they are called float.
This is the Python code to receive the price as input from the keyboard.
The float() function converts the string input into a float input.
As with the integer data type, units are not stored. Sometimes a price can be an
integer, although currency is most often stored as a real data type. For example,
the price of cars is often very high and so the prices will not include pence – in this
case the data can be stored as integers.
© paullong.net 2020 Page 12 of 154 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 6 - Published by paullong.net
Boolean
Boolean data can only have one of two values. These values are “True” or “False”.
This can also be represented as “Yes” or “No”.
This is the Python code to receive passed_exam as input from the keyboard.
The bool() function converts the string input into a Boolean input. If any data is
input then the value will be True. If no data is input (e.g. "") then the value will be
False.
A common misconception is that Boolean data types include text fields where
data can be only two single character values, such as “M” or “F” for Male or
Female. However, this is not correct because they are not “Yes” or “No” / “True” or
“False” values. To be a Boolean data type, a person’s gender would have to be
stored as “Are you a male?” – with an answer of “Yes” or “No”. Just because a
field has two possible answers does not make it Boolean. The following examples
are not Boolean, they are string:
2) Identify the most appropriate data types for each of the following variables
used when collecting data about a house:
a) number_of_bedrooms b) has_a_garage
c) post_code d) price
e) estate_agent_name f) has_a_swimming_pool
Casting
Sometimes data starts life as one data type but needs to be converted to a
different data type for further manipulation. For example, if a number has been
extracted from a string and now needs to be used within a calculation, it will need
to be a real or integer to perform a calculation on it.
Example – casting
a = int("484") # evaluates to 484
b = float("359.20") # evaluates to 359.20
c = float(20) # evaluates to 20.0
d = str(90) # evaluates to "90"
e = bool(0) # 0 always evaluates to False
f = bool(1) # 1 always evaluates to True
g = bool("Any Text") # any string value always evaluates to True
h = bool("") # blank string always evaluates to False
i = list("hello") # evaluates to ["h", "e", "l", "l", "o"]
In Python, all data that is input from the keyboard is received as a string. Therefore,
if it needs to be used as a number, then it needs to be cast to an integer or a float.
In Python, if you want to concatenate (join) variables together, then they need to
be the same data type.
Example – coercion
x = 3
y = 4
y = x + 5.5
x += 2.4
Activity – casting
Questions – follow me
1) Describe the difference between a character data type and a string data
type. [2]
2) Describe the difference between an integer and a real data type. [2]
5) A user is asked to input the cost of a holiday. Using an example related to the
cost of the holiday, explain why casting might be necessary in Python. [3]
• addition
• subtraction
• multiplication
• division
Brackets
Indices
Division
Multiplication
Addition
Subtraction
© paullong.net 2020 Page 17 of 154 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 6 - Published by paullong.net
Calculations within brackets should always be calculated first. After that, any
indices (exponent) calculations should take place, followed by
division/multiplication, then addition/subtraction – in that order.
Integer division uses // and will give the result as the whole number part of a
division.
To calculate the remainder, % can be used to give the remainder part. This is
known as the modulus.
Example – remainder
total = 6 % 3 6÷3=2r0 so 6 % 2 = 0
total = 16 % 5 16 ÷ 5 = 3 r 1 so 6 % 5 = 1
d = 85 % 10 85 ÷ 10 = 8 r 5 so 85 % 10 = 5
y = 114 % 5 114 ÷ 5 = 22 r 4 so 114 % 5 = 4
z = z % 8 the remainder will be between 0 and 8.
2) Write code to calculate the following and assign to the variable <answer>:
a) The whole number part of the integer division of 200 ÷ 9.
b) The whole number part of the integer division of 372 ÷ 7.
c) The whole number part of a number chosen by the user ÷ 7.
d) The whole number part of a number chosen by the user divided by
another number chosen by the user.
3) Write code to calculate the following and assign to the variable <answer>:
a) The remainder when 393 is divided by 16.
b) The remainder when 901 is divided by 19.
c) The remainder when 1000 is divided by a number chosen by the user.
d) The remainder when a number chosen by the user is divided by another
number chosen by the user.
Exponent operator
The exponent is often referred to as the index or power.
52 = 5 x 5 = 25 53 = 5 x 5 x 5 = 125 54 = 5 x 5 x 5 x 5 = 625
Example – exponent
total = 2**3 23 = 2 x 2 x 2 so total = 8
total = 4**2 42 = 4 x 4 so total = 16
x = 5**4 54 = 5 x 5 x 5 x 5 so x = 625
Activity – exponent
1) Predict the value of answer in each of the following:
a) answer = 5 ** 2
b) answer = 2 ** 7
2) Write code to calculate the following and assign to the variable <answer>:
a) 6 to the power of 2.
b) 4 to the power of 3.
c) 2 to the power of 4.
d) 5 to the power of a number chosen by the user.
e) A number chosen by the user to the power of another number chosen by
the user.
Mathematical functions
This will round a number to the number of decimal places indicated by dp.
Example – round
answer = round(25.876, 2) 25.876 to 2dp so answer = 25.88
Activity – round
1) Predict the value of answer in each of the following:
a) answer = round(30.253, 2)
b) answer = round(138.253892, 4)
2) Write code to calculate the following and assign to the variable <answer>:
a) 27.1028 rounded to 3 decimal places
b) 18.00042 rounded to 4 decimal places
c) 120.2 rounded to 0 decimal places.
3) Did you notice that the value of the answer for 2(c) actually displayed as one
decimal place even though you had asked it to round to zero decimal
places?
a) Explain why has this happened?
b) Modify the code from 2(c) so that it does not display any decimal places.
Extension:
4) Predict the value of answer in each of the following:
a) answer = round(4.5)
b) answer = round(7.5)
c) answer = round(8.5,0)
d) answer = round(11.5,0)
e) answer = round(4.15,1)
f) answer = round(4.25,1)
g) answer = round(4.35,1)
The range function can be used to produce a list of numbers within a range:
range(start, stop, step)
This will produce a list of numbers in a range starting at value start and ending at
the value before stop. For example, if stop is 10, then the ending value will be 9.
If a number is entered for step, then this will be the gap between each number in
the range. If a number is not entered for step, then the gap will be one (1) by
default.
Example – range
In these examples, the list function has been used to generate a list from the range
of values. You will learn more about the list function in the using lists section.
answer = range(1, 5)
print(list(answer)) this will produce [1, 2, 3, 4]
Notice how the range of values stops at 4, which is one below the stop value of 5.
The stop value in the range function is not inclusive, but the start value is.
In the example above, the gap between each number has not been specified
and so it is set to the default of 1. In the example below you can see how
including the step of 1 gives the same result as above.
answer = range(0, 51, 10) this sets a gap of 10 between each number
print(list(answer)) this will produce [0, 10, 20, 30, 40, 50]
Activity – range
1) Predict the value of answer in each of the following:
a) answer = range(0, 10)
b) answer = range(1, 15, 2)
c) answer = range(100, 190, 10)
import math
The math functions that you need to be able to use for Edexcel are:
math.pi gives the value of Pi to 15 decimal places
math.sqrt(number) calculates the square root of a number
math.floor(number) returns the largest integer value less than a number
(similar to rounding down to zero decimal places
for positive numbers)
math.ceil(number) returns the lowest integer value higher than a
number (similar to rounding up to zero decimal
places for positive numbers)
pi = math.pi = 3.141592653589793
The code below finds the largest integer value less than 25.67:
answer = math.floor(25.67) = 25
The code below finds the lowest integer value higher than 25.67:
answer = math.ceil(25.67) = 26
2) Run the code above. Were your predictions correct? What was unexpected
or noticeable about the answers to 1f and 1g?
3) a) Modify the code for 1d so that it casts the float value 5.99 to an integer.
You may find the int function helpful.
b) Does this give the same result as 1d?
c) Modify the code for 1f so that it casts the float value -5.6 to an integer.
d) Why does this give a different result to 1f?
4) Write code to calculate the following and assign to the variable <answer>:
a) The square root of -49
b) The value of Pi to 2 decimal places
c) The floor value of 119.52
d) The ceiling value of 2.08
e) Casting the float value of -6.8 to an integer
f) Rounding the float value of -6.8 to 0 decimal places
g) The floor value of -6.8.
You have probably noticed by now that for positive float (real) numbers, the int
function does exactly the same as the math.floor function. However, it works
slightly differently. The int function truncates the decimal values. The math.floor
function rounds down to the nearest whole integer.
Imagine you through the dice 10 times. It is possible, although not probable, that
this could happen:
You could also throw 10 ones or 10 twos, but it is more likely there will be a mixture
of numbers.
The probability of throwing any single number is one in six or 1/6 because there are
six numbers in total.
To use random functions you will need to import the random module in Python:
import random
To generate a random floating point number (real) between 0.0 and 1.0, use the
following code:
random.random()
The start variable represents the lowest integer the random number can be. The
lowest integer is zero (0). The end variable represents the highest integer the
random number can be. You will need to import the random
Video
Extension: Watch http://tiny.cc/randomnumbers about how computers can use
the time to generate random numbers from YouTube.
Questions – follow me
3) Write code to calculate how many slices of pizza each person will get and how
many slices will be left over. The program should ask the user for the number of
people, the number of pizzas and the number of slices per pizza. [4]
5) List all the possible values of <answer> in the code below: [3]
a) answer = random.randint(0, 5)
b) answer = random.randint(0, 10) * 10
6) Write code to ask the user how many sides they want on their dice (e.g. 12).
Roll an imaginary dice and return a value randomly. [2]
4) String manipulation
A string is a set of characters. “Programming” is the set of characters “P”, “r”, “o”,
“g”, “r”, “a”, “m”, “m”, “i”, “n”, “g”.
Length
The length of a string is the number of characters within the string. This can be
calculated in Python using LEN:
len(stringName)
Concatenation
Concatenation is the process of joining. To concatenate two strings is to join two
strings. The + symbol is used to concatenate two or more strings.
Example – concatenation
surname = "MacDonald"
forename = "Ronald"
fullname = forename + " " + surname
Predict what you think the value of answer will be in the following
when the values of 20 and 40 are input:
Did you predict 60? Run the code and see what the result is. Why do you think the
answer was different to 60?
Slicing
Slicing is the process of extracting part (a slice) of a string. For example, “gram” is
a slice of “Programming”.
Index (position) 0 1 2 3 4 5 6
Value P r o g r a m
character = string[position]
The value of <initial> will be “M” because M is the first character which is
position 0 (indexing starts at 0 in Python).
what do you think the value of letter will be in the code below?
letter = "Computer"[4]
The example above returns a single character within a string. Slicing can also
return several characters from a string:
Like with the range function, the end index is not inclusive of the value at that
position. To count from the end of the string instead of the start of the string,
negative numbers can be used.
Example – slicing
what the value of answer will be in each line of code below.
surname = "MacDonald"
answer = surname[0:3]
answer = surname[3:6]
answer = surname[3:]
myString = "Comprehension."
answer = myString[8:]
answer = myString[:4]
answer = myString[-1:]
answer = myString[-4:]
answer = myString[:-1]
answer = myString[:-6]
answer = myString[-6:-4]
ASCII
Note: You may find it useful to read chapter 2 section 4 about data representation
using ASCII before this part.
ord(character)
chr(code)
The value of the character variable should be any single character and the value
of the code variable should be any integer value in the range used for Unicode.
1) Using an ASCII table, predict the value of answer in each of the following
lines of code:
a) answer = chr(52)
b) answer = chr(163)
c) answer = chr(9) + "."
d) answer = ord("q")
2) Write code that uses character code conversion to produce the following:
a) the ASCII / Unicode value of T
b) the character {
Field size
The amount of space that data uses up on screen is determined by its width (or
field size). For example, the word “Programming” would use up 11 characters so
has a width (or field size) of 11. We are going to look at why this makes it difficult to
put data into a tabular format.
This code will work through each student but the important part you need is the bit
that displays the data on the screen:
the code and you will find the output of this data looks like this:
This is very difficult to read and ideally we would like it to look like this:
When you run this code, all it does is to move the text to the next tab position. With
some long and short names, this does not help, as the data is still difficult to read:
Instead, we can use the format method to specify a fixed width for each item of
data. In this example, we will fix the width as follows:
forename 15 characters
surname 20 characters
tutorGroup 3 characters
When you run the code you can see the output looks like what we were hoping
for:
The output is set out in columns. The table below shows how columns 1-15 (width
of 15) are used for the forename, 16-35 (width of 20) are used for the surname and
36-38 (width of 3) are used for the tutor group.
1 5 10 15 1 5 10 15 20 1 2 3
Columns 1-15 (forename) Columns 16-35 (surname) 36-38
C h e l s e a B a r t h o l o m e w 1 0 B
A m y A r g u e 1 0 C
A n n - M a r i e F e r g u s o n - A n d r e w s 1 0 C
K a p i l J a t i l Y 1 0 B
2) What is the smallest width you can use for forename and surname before
the output is not displaying properly?
3) Try changing the order in which each item of data about the student is
displayed on each row.
print("{:15}".format("Hello World"))
print("{:25}".format("Hello World"))
print("{:5}".format("Hello World"))
Did you notice that none of these made any difference to a single string? That is
because we cannot see the space that is being used.
The code has now been adapted to include some additional text “---field ends
here” to show the effect the field width has:
Did you notice that even though a field size of 5 was used, the whole of “Hello
World” was displayed? This is because the field size is a minimum width rather than
a fixed width.
© paullong.net 2020 Page 35 of 154 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 6 - Published by paullong.net
Alignment
The field size becomes a lot more useful when aligning data. We can align data to
the left, centre or right within a width (field size). The following codes can be used
for alignment:
Example – alignment
Run the code below to investigate how the word “Hello” can be aligned within a
field size of 50:
print("{:<50}".format("Hello"))
print("{:>50}".format("Hello"))
print("{:^50}".format("Hello"))
The example above uses “Hello” as a literal constant but a variable containing a
string could also be used:
Investigate how this code aligns strings by inputting different string values, e.g. “This
is my string”.
The example above will align the data within the field size, but it is difficult to see
the unused space. We can fill the unused space with characters. This is known as
padding. To fill the unused space, simply enter the character that should be used
before the alignment symbol.
Example – padding
Run the code below to investigate how the hyphen has been used as padding:
print("{:-<50}".format("Hello"))
print("{:->50}".format("Hello"))
print("{:-^50}".format("Hello"))
Continue to run the next part of the code to see how an asterisk (*) has been used
instead of a hyphen:
the code to use different characters for padding, different field sizes
and different alignments. See if you can create
xxxxxxxxxx
xxxxxxxx
xxxxxx
xxxx
xx
xxxx
xxxxxx
xxxxxxxx
xxxxxxxxxx
2) Write code using the format method with alignment and padding to create
the rhombus shape below:
oooooooooooooooooooooooo ooooooooooooooooooooooooo
ooooooooooooooooooooooo oooooooooooooooooooooooo
oooooooooooooooooooooo ooooooooooooooooooooooo
ooooooooooooooooooooo oooooooooooooooooooooo
ooooooooooooooooooooo oooooooooooooooooooooo
oooooooooooooooooooooo ooooooooooooooooooooooo
ooooooooooooooooooooooo oooooooooooooooooooooooo
oooooooooooooooooooooooo ooooooooooooooooooooooooo
3) Experiment using the format method with different alignments, field widths
and padding to create different shapes.
Formatting numbers
You saw in the data types section how to cast an integer to a real and a real to an
integer, and you have also seen how to use integer division, round, floor and ceil
functions. All of these will change the data. However, there are times when you
just need to be able to format a number so that it looks right. The format method
can be used to format float (real) numbers to a specific number of decimal places
or to align numbers.
print("{:.0f}".format(3.14159259))
print("{:.2f}".format(3.14159259))
print("{:.10f}".format(3.14159259))
Did you notice that if more decimal places are specified than exist, then there are
trailing zeros?
Run the code below to see how the number of decimal places (fixed point) can
also be used within a field size (width). This enables numbers to be aligned on the
decimal point:
print("{:8.4f}".format(3.14159259))
print("{:8.4f}".format(485.67921928))
print("{:8.4f}".format(28.6))
This can be particularly useful when dealing with currency. Run the code below
and notice how the £ sign has been included in the output before the formatting
of the numbers:
print("£{:8.2f}".format(19.62))
print("£{:8.2f}".format(284.91))
print("£{:8.2f}".format(1.2))
1) What do you think will happen if you run the code below?
a) How will the numbers be formatted?
b) Will all the numbers be displayed?
c) How will they be aligned?
print("{:8.2f}".format(284.913))
print("{:8.2f}".format(1.2))
print("{:8.2f}".format(102956748.6))
2) Run the code and see if your prediction was correct. Discuss with others in
your class why you think this has happened.
4) Modify the code below to display the numbers in a column aligned by the
decimal point:
print(104.23)
print(92.2467)
print(8.001)
print(5291)
The d option can be used to align integers to the right within a field size (width)
and to also cast the integer to a string.
x = 10
y = "{:d}".format(x)
print(y*2)
the code. Was your prediction correct? What do you think has
happened?
The d setting means that the integer is cast to a string. Therefore, two lots of 10
(one zero) are 1010 (one zero one zero).
The d setting also aligns the data to the right within a field size (width). Run the
code below and then modify it to use different widths.
Run the code below to investigate how the number of decimal places (fixed
point) has been set:
print("{:5d}".format(x))
print("{:5d}".format(10 * x))
print("{:5d}".format(100 * x))
print("{:5d}".format(1000 * x))
Predict what the code below will do. Run the code to investigate what it does.
Modify the code to see what happens when you use different numbers and
decimals.
print("{:,}".format(10000000))
2) Modify the code below to display the numbers input by the user to the right:
3) Try inputting long numbers. Discuss what limitations there might be to your
modified code from question 2.
Positional arguments
One method of including multiple items of data in a single print statement is to
send them as multiple arguments (parameters). Another method is to use the
format method with positional arguments (parameters).
firstname = "Paul"
score = 5
teacher = "Mrs Robinson"
One way of doing this would be to use multiple arguments within a print statement:
Another way of doing this is to use positional arguments using the format method:
The Edexcel Programming Language Subset (PLS) guide includes this example:
Notice how each value has only been used once and so it is not necessary to
reference them as 0, 1, 2. Also notice how each positional argument has been
formatted.
Experiment by modifying the three sets of code above to see what happens when
you make changes to the values, formats and order of variables.
Case conversion
It is possible to change the case of data using the upper and lower methods.
myString = "hello".upper()
print(myString)
Changing the case can be useful when comparing user inputs. The code below is
designed to decide if a user would like to quit a program:
Search functions
There will be occasions when you need to find a substring within a string or replace
a substring with another string. The functions below allow you to search for a
substring within another string:
Example – find
The code below will search the word “Hello” for the value “e”. If “e” exists, it will
return its position (1):
answer = "Hello".find("e")
myWord = "Programming"
answer1 = myWord.find("gram")
answer2 = myWord.find("me")
Activity – find
Write code to:
2) Ask the user for a word or phrase that they would like to search. Then search
for “the” within that word or phrase.
3) Ask the user for a word or phrase that they would like to search. Ask the user
for a substring that they would like to find. Then search for the substring
within the word or phrase.
The index function can be used in a similar way to find, but if the substring is not
found within the string, then an exception (error) is raised instead of -1.
Activity – index
Open the code used in the example for the find function:
myWord = "Programming"
answer1 = myWord.find("gram")
answer2 = myWord.find("me")
the code so that index is used instead of find for each example.
Experiment with the code below using different values for myWord and
mySubString:
Which function (index or find) will be the more robust to use when the string to
be searched and/or substring to be found are unknown? Justify your answer using
the code above as an example.
The replace function can be used in a similar way to find, but if the substring is
found, then it is replaced with another substring.
Example – replace
The code below will search the word “Hello” for the substring “lo”. If “lo” exists, it
will replace it with “p” and return “Help”:
Activity – replace
Open the code used in the example for the find function:
The strip function can be used to remove characters from the beginning and
end of a string. It will strip each character individually.
Example – strip
The code below will search for the characters “,” and “.” in the string
"...Hello,,," and remove them from the beginning and end of the string.
answer = "...Hello,,,".strip(",.")
The value of answer will be "Hello" because the commas were replaced from the
end and the full stops from the beginning.
The default character to strip is white space which can be useful for removing any
white space at the beginning and end of a string value. When removing white
space, there is no need to specify a character.
Remember that the strip function will only strip values from the beginning and
end of a string.
Activity – strip
the final value of fruit in the code below:
fruit3 = ",,x,,bananab,x,"
fruit3 = fruit3.strip(",xb")
print("Of all fruits", fruit3, "is my favorite")
Write code to strip any punctuation from the beginning or end of a sentence input
by the user.
Extension: use the replace function to strip any punctuation from anywhere in
the sentence.
Example – split
The code below will search for commas. It will then create a list of all the values
between the commas.
answer = "One,Two,Three".split(",")
Activity – split
the final value of x in the code below:
The split and strip functions are very useful when importing a comma
separated- values (CSV) file to separate values at each comma and to strip the
line breaks to turn the file into a two-dimensional list. You will learn more about
reading CSV files later.
Questions – follow me
1) State the value of <answer> in each of the following sets of code: [4]
a) answer = len("Hello world")
b) answer = "Hello world"[4]
c) answer = "Hello world"[3:5]
d) "Hello world"[-1:]
4) Write code to find the last 3 characters of any word or phrase input by a user.
[4]
A student’s username is calculated by the last 2 digits of the year they started
school, the first 3 letters of their forename and the first 3 letters of their surname. All
letters must be lowercase. For example, Joseph High who joined the school in 2015
would be 12joshig.
5) Write code to create a student’s username when their first name, surname and
year they joined the school are input. [5]
A part number consists of the last digit of the year of manufacture, the first 2
characters of the manufacturer’s name and the last 3 digits of the stock code. All
letters must be upper case.
Extension:
A cipher is an algorithm that encrypts text so that it cannot be understood without
the decryption key. A cipher algorithm is used to encrypt a string “This is my
original text”. The cipher algorithm converts each character in the string to its
ASCII code and then adds 4 to that code. Each code is stored in a 6-digit format
using leading zeros when required.
5) Programming concepts
Sequence
A computer program can run in sequence – that is from start to finish:
Display
Display “Hello
START “What is your Wait for User Input STOP
<user input>”
name?”
Each instruction takes place in order. If the order is wrong, the program may not
run as expected.
Sleep
The sleep procedure can be used within a sequence of commands to pause a
program. This could be useful to give the impression that something is happening
in the background or to slow down the speed that the computer processes data.
To use the sleep procedure, you will need to import the time library:
import time
You can then specify the number of seconds that you want the computer to
pause for:
time.sleep(seconds)
Example – sleep
The code below asks the user questions and pretends to be able to answer them.
Sleep is used to give the impression that somebody is thinking.
import time
Run the code and watch what is happening as you answer the questions.
Selection
Selection is one method of controlling the flow of the program. The idea of
selection is that some program statements will only run sometimes. A question is
asked that requires an answer of true or false and the program will take one of two
different directions depending on the answer.
No
if condition is true:
run these statements
else:
run these statements
Note: the #endif comment is not required, but it helps to follow code more easily.
It is not always necessary to have an else part to the selection. It is possible just to
have a then part alone:
if condition is true:
run these statements
Example – if . . then
No
high_score = 20
score = int(input("What is the score? "))
Video
Watch the http://tiny.cc/bbcselection from BBC Bitesize and read the example
below the video.
Is <mark>
START Yes Grade ← “Pass” STOP
over 50?
No
Grade ← “Fail”
b)
No
Multiple options
if . . then . . else only allows for two options – a condition is TRUE route and a
condition is FALSE route. This is fine when only two routes are needed, but often
more than one route is needed. For example, you might want to display the name
of the day of the week based on a number from 1 to 5. There are 5 options and so
a normal if . . then . . else will not work.
Example – else if
To determine the day of the week based upon its number:
day_number = int(input("Enter a number for the day of the week
between 1 and 7: "))
if day_number == 1:
day_name = "Monday"
elif day_number == 2:
day_name = "Tuesday"
elif day_number == 3:
day_name = "Wednesday"
elif day_number == 4:
day_name = "Thursday"
elif day_number == 5:
day_name = "Friday"
else:
day_name = "Weekend"
#endif
Activity – else if
Write code for each of the following algorithms:
1) Display the words “One”, “Two”, “Three” for user input of numbers 1-3 or “Not
valid” for any other number.
2) In a test, students who score less than 40 will fail. From 40 to 59 is a pass, from
60 to 79 is a merit and 80 or above is a distinction. The score is stored in a
variable named <score> and the grade should be stored in a variable
named <grade>.
3) Display a person’s gender based on the input of their title of Mr, Mrs, Miss or
Ms.
Extension: draw flow charts for each of the above algorithms.
You may have noticed that elseif is really just a case of nesting another if block
within the else section. It could also be written like this:
if condition is true:
run these statements
else
if condition is true:
run these statements
else
if condition is true:
run these statements
else
run these statements
Nested selection
Sometimes, another if . . then . . else block needs to be nested inside another IF
block. This is known as a nested IF.
if condition 1 is true:
if condition 2 is true:
run these statements (a)
else:
run these statements (b)
#endif condition 2
else:
run these statements (c)
#endif condition 1
Example – nested if
This works out whether a person is a man, woman, boy or girl:
if status == "Adult":
if gender == "Male":
description = "Man"
else:
description = "Woman"
#endif
else:
if gender == "Male":
description = "Boy"
else:
description = "Girl"
#endif
#endif
Activity – nested if
Write code for each of the following algorithms:
1) If a sheep is an adult then females are known as ewes and males are known
as rams. Otherwise baby sheep are known as lambs. The variable <adult>
can either be True or False and the variable <gender> can either be “M” for
male or “F” for female. Complete the code in the Python file.
2) Some rubbish can be recycled. If the rubbish is recyclable then if it is paper it
should be put in the paper bin and if it is plastic then it should be put in the
green bin. All other rubbish should be put in the grey bin. The variable
<recyclable> can be True or False and the variable <rubbish_type> can
be “Paper”, “Plastic”, “Nappies” etc. Display the colour of bin that
should be used. Complete the code in the Python file.
3) The user is asked to input two numbers. If <number 1> and <number 2> are
the same, then display “Numbers match”. If <number 1> is bigger than
<number 2> then display “Number 1 is bigger”. If <number 2> is bigger
than <number 1> then display “Number 2 is bigger”.
4) If <input_integer> is 0 then display “Zero”. If it is negative, then display
“Negative” and if it is positive display “Positive”.
Iteration
Iteration is a posh word for repetition.
In programming, iteration is about
loops. It means that a set of
instructions can be repeated without
writing them out again. Instructions
can be repeated a set number of
times which is known as a count-
controlled loop.
Note: Edexcel use the wrong terminology for iteration. Iteration actually refers to
any repetition of code using either count-controlled or condition-controlled
loops. Edexcel’s specification wrongly uses the terms repetition and iteration
to mean different things. Edexcel refers to iteration as only meaning
iterating through items in a data structure which is a wrong interpretation.
You should however be aware of Edexcel’s interpretation for the exam.
Video
Open http://tiny.cc/BBCiteration from BBC Byteszie.
1) Watch the video about iteration.
2) Listen to how zoologist Bill Sellars uses controlled loops
3) Read about infinite loops.
With count-controlled loops, the number of iterations (repeats) is known before the
loop starts. For loops are count-controlled loops.
With condition-controlled loops, the number of iterations is NOT known before the
loop starts. The loop will continue while/until a condition is true. The loop will
involve an instruction that assigns a value to a variable that is part of the condition
of the loop. While loops in Python are condition-controlled loops.
For loop
A for loop is a count-controlled loop. It will repeat a specified number of times. In
Python, a for loop will repeat for each item in an iterable (something that can be
iterated). This is particularly useful for lists such as arrays and files which you will
learn about later in this chapter.
This for loop will work with each character in the string named mystring. It will
send each character to the display one at a time and add a hyphen to each
character. The part that is end="" simply stops the console from creating a new
line.
H-e-l-l-o- -W-o-r-l-d-
The comment #next character is not needed, but it helps to understand what is
happening with the code.
2) Remove any spaces from a phrase input by the user. You will need to use an
if statement within the for loop and assign each character to a new string,
missing out the spaces. Complete the code that has been started for you in
the Python file.
A for loop can also repeat a set number of times. In Python, for loops only work
through each item in an iterable. Therefore, a range of values is created as a list
so that each value in that list can be iterated through.
for variable in range(start, stop):
repeat these statements
This for loop will repeat 5 times. The variable named <counter> is assigned the
value 1 for the first time through the loop. <counter> then moves through each
value in the list until it gets to 5. This loop will display the numbers 1 to 5 on the
screen:
1 2 3 4 5
This adds one to the value of counter each time and so performs the same job as
working through a list from 1 to 5.
Note: remember that range goes up to but not including the stop value.
Then it is displayed on
screen.
It is increased by 1
(incremented by 1) – next
item in the range.
This for loop will repeat 3 times through [1,2,3]. Each time the user will be asked
to input a word. The word is then displayed on the screen together with which
number word it was, using the value of counter:
The word is also added to a new word so all 3 words appear as one:
AppleBananaCherry
So far, the for loop has had a fixed start number and a fixed end number. It is
possible to replace the start and end numbers with variables.
This for loop will repeat the number of times decided by the user. The variable
named <endnum> is assigned the number chosen by the user and the loop runs
until it gets to the value of <endnum>. If the user enters the number 6, the following
will be displayed on the screen:
4 8 12 16 20 24
If the user enters the number 20 followed by the number 24, the following will be
displayed on the screen:
60 63 66 69 72
So far you have seen how the for loop can be used to count one number at a
time. It is also possible to set the for loop to count multiple numbers at a time using
the step option in range:
This for loop will start by displaying 1 and will then keep adding 2 until 11 is
reached. It displays all odd numbers between 1 and 11. The output will be:
1 3 5 7 9 11
It is not really the for loop that is adding 2 each time, but the range function which
has created a range from 1 to 11 in steps of 2.
This for loop will step up in values of the multiple chosen by the user. It will start at
the multiple chosen by the user (e.g. 5) and then keep adding the multiple (e.g. 5)
until it reaches 10 times the multiple’s value (e.g. 50). It displays a times table. If
the user input 5 as <multiple> then the output will be:
5 10 15 20 25 30 35 40 45 50
While loop
The simplest while loop is one that repeats forever. This flowchart shows a loop that
will never end:
import time
while True:
print("Red")
time.sleep(5)
print("Red/Amber")
time.sleep(1)
print("Green")
time.sleep(5)
print("Amber")
time.sleep(2)
#end while
While a number that is not between 1 and 10 is chosen, the loop will be run
displaying an error message and asking for a number again. If the user chooses a
number between 1 and 10 straight away, then the WHILE loop will never run.
This while loop displays the real numbers 200 to 100. 5 in reverse order, including
half values:
counter = 200
while counter > 100: Can you code this while loop as a for loop
print(counter) instead? Hint: you will need a negative
value for step and step must be an integer
counter = counter - 0.5
so you will need to divide the output by 2.
#endwhile
© paullong.net 2020 Page 63 of 154 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 6 - Published by paullong.net
Write code using WHILE loops for each of the following algorithms:
2) Ask the user for a number. While the number is still at least 1, keep dividing it
by 5. Display the final value of the number.
3) Ask the user for a number. Add up all the numbers between 1 and that
number. For example, if the users gives 4, then add 1 + 2 + 3 + 4.
4) Ask the user for a number between 55 and 65. If the user enters an invalid
(out of range) number then display an error message and ask them to enter a
number again.
5) Ask the user for a password. The password must be “letmein”. If the password
is wrong then inform the user and ask for the password again.
Nested loops
A nested loop is a loop inside another loop. It
START
is possible to have a for loop inside a while
loop or similarly any loop can be nested
inside any other loop.
Is condition
No
1 true?
This example shows a while loop nested inside
another while loop. While condition 1 is true, Yes
the outer loop will run. If condition 2 is also
true, then the inner loop will run repeatedly Run instructions
STOP
in loop 1
while condition 2 is true. When condition 2 is
no longer true, the outer loop will then
Yes
continue until condition 1 is no longer true.
Is condition
2 true?
Yes
Run instructions
in loop 2
No
The outer for loop above runs 5 times. For the first run through, counter1 has the
value of 1. Therefore, the inner for loop only runs once and displays the value of
counter1 which is 1. For the second run through, counter1 has the value of 2.
Therefore, the inner for loop runs twice and displays the value of counter1 twice.
The outer for loop above runs 5 times. The first inner for loop will run the number of
times calculated by 5 minus the value of counter1, which on the first run through
will be 4. Therefore, 4 dots are displayed before 1 is displayed.
55555
666666
7777777
88888888
Replace ### in the code below with numerical values and/or variables so that it
displays the above values to the screen:
Write code using nested loops for each of the following algorithms:
2) Display the following:
1
22
333
4444
Questions – follow me
1) Examine the Python code below. The user will be asked to input values for x
and y.
A) for a in range(1, 11): print("Yes")
B) while x == y: print("Yes")
C) if x == 5: print("Yes")
D) while x == 4 and y == 5:
print("Yes")
x = int(input("Please input a number: "))
b) Change one line of code to display how many lives are left. [1]
[ TURN OVER ]
3) Line numbers are used for the algorithm below but are not part of the code.
4) Line numbers are used for the algorithm below but are not part of the code.
The algorithm calculates the amount of discount to apply for a holiday.
a) Calculate the value of discountedPrice and show your working when the
price and discount code entered are:
(i) 100 and b [2]
(ii) 600 and a [2]
b) Add code to lines 11 and 12 to assign the value 0 to the discount code if
one of a, b or c are not entered. [2]
6) Ask the user for a number between 0 and 200. If the number is below 50,
display "Too high". If it is smaller than 50, display "Too low". Keep repeating until
the user enters 50. Display how many attempts it took the user to guess the
number.
7) A game of rock, paper, scissors. The computer will ask the user to input “Rock”,
“Paper” or “Scissors”. The computer then randomly generates its own answer
from those 3 options. Paper beats Rock (scrunches it up), Rock beats Scissors
(makes them blunt) and Scissors beat Paper (cuts it up). The computer should
state who won and what the computer chose.
6) Operators
Relational operators
Relational operators are used to compare two values. These are the relational
operators and the symbols that you need to be familiar with:
equal to ==
not equal to !=
less than <
greater than >
less than or equal to <=
greater than or equal to >=
In the if example below, b is compared with a to see if it is greater than a. Can you
spot anything wrong with the ELSE part?
b = 0
while b < 10:
print(b)
b = b + 1
#endwhile
Logical operators
A logical / Boolean operator returns a response of True or False. They are used in
conditions.
not condition must not be true
and two or more conditions must both be true
or either one of two conditions must be true
The code below will repeat while both conditions are true:
lives = 3
score = 0
1) Ask a user for a password and keep asking for the password while it is not
“letmein”. Do not use the != operator.
2) Display two numbers to the user. Ask the user to multiply them together then
ask the user to divide the first by the second. If both answers are correct
then display “Well done”, otherwise display “Whoops”.
3) Ask the user to enter their gender. If they enter any of “M”, “m”, “Male” or
“male” then display “you must be a man”.
4) Ask the user for 3 numbers - a, b and c. If a is zero or both b and c are 0
then display “You cracked the code”. Keep asking until the code is
cracked.
Questions – follow me
1) Draw lines to connect the symbols to each relational operation. [6]
a) = greater than
b) > less than
c) <> equal to
d) <= not equal to
e) >= greater than or equal to
f) < less than or equal to
a) 298 ❑ 300
b) 1,987 ❑ 1,987
c) 565 ❑ 560
4) a) Complete the code below so that it continues until either p is greater than
256 or equal to 256. [1]
p = 1
while p < 256:
print("The next number is", p)
p = p * 2
7) Data structures
Arrays
Earlier in this chapter you learned about how to use variables and set their data
types. A variable can hold a single piece of data. If you wanted to record the
names of several people, you could use variables like this:
name1
name2
name3
name4
name5
Then to collect the data you could assign user input to those variables:
name1 = input()
name2 = input()
name3 = input()
name4 = input()
name5 = input()
This becomes very repetitive. It also only works if you know how many names to
collect and makes it more difficult to process the data stored in the variables
An array is a variable with a single identifier that can hold multiple items of data in
memory. As well as having an identifier name, an array has an index which refers
to the position in the array. Data in an array must all be of the same data type.
Example – array
This array stores people’s surnames. It is called surnames:
The array has had 4 values assigned to it which are all string values.
To access these values, the position in the list needs to be identified. The position is
known as the index and starts at 0 (zero).
Note: the index always starts at 0 (unless you are told otherwise).
© paullong.net 2020 Page 75 of 154 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 6 - Published by paullong.net
In the example above, the data can be represented like this:
Position / Index 0 1 2 3
Value Long Boden Abraham Schmidt
The number of items in an array can be found by knowing the length of the array:
number_items = len(array)
Arrays must have data of the same data type (homogeneous). We will look at lists
later which can have data of different data types (heterogeneous). Python
implements arrays using lists.
The example above assigns values to the array. It is also possible to assign
variables or input values to an array. When accessing or assigning lots of values, a
loop can be used to assign values one at a time.
The variable counter changes in value from 0, 1, 2 to 3 as the for loop runs.
The example below shows how items in an array can be assigned values using
input from the keyboard:
This can still be inefficient if the number of inputs for the array is not known or if
there are a lot of inputs. Therefore, a for loop or another loop can be used to
cycle through the array using an identifier for the index position:
surnames = [None]*4 # initialises array with 4 values
for counter in range(0, len(surnames)):
surnames[counter] = input("Please input a surname: ")
#next surname
print(surnames)
The reason for this is beyond GCSE level but ask your teacher if you want to know.
2) Modify the code to assign 3 numbers of the user’s choice to an array and
then display the numbers to the screen. You should calculate the length of
the array for the range stop value.
When collecting data for an array, the number of items for input might vary. For
example, the user could be asked how many items they want to input.
The user may not know how many data items they are inputting and so could be
asked to input a special character when they have finished.
It is also possible to search for data in an array. Using a linear search (chapter 1),
each item is checked one at a time until the data being searched for is found or
the end of the array is reached.
counter = 0
found = False
while counter < len(array_name) and not found:
if array_name[counter] == item_to_find:
found = True
print("Item was found at position", counter)
else:
counter = counter + 1
#endif
#endwhile
The index (position) of the item being looked for is now known and can be used.
You already know how to find the length of an array which will tell you how many
elements there are in it. If an array holds numbers then it is also possible to find the
maximum, minimum and average of all the elements. Python has special
functions to do these, but you could also write your own code to do these.
Each time an element of the array is read, it is added to the total score until all
elements have been added together.
min_score = scores[0]
for score in scores:
if score < min_score:
min_score = score
#endif
#next score
1) Modify the code from the minimum score example above to find the highest
value in an array.
2) Write code to calculate the average value in an array without using the
built-in statistical functions.
Lists
Lists are very similar to arrays. Python does not use arrays, and so any
implementation of an array in Python is a list. There are two main additional
features of a list:
Some list methods and functions that you will find useful include:
Example - lists
The code below creates an empty list:
myList = list()
what the contents of myList will be after the following code has run:
myList.append("Volvo")
myList.append("Ford")
myList.append("Vauxhall")
what the contents of myList will be after the above code has run.
The code below will insert an item into a list at the index position given:
myList.insert(1,"Toyota")
print(myList)
what the contents of myList will be after the above code has run.
Activity – lists
A list contains the numbers below:
myList = [21, 40, 81, 30, 27, 18, 26, 41, 43, 80]
Two-dimensional arrays
Video
Watch http://tiny.cc/arraybbc about arrays from Ed Tracey’s BBC Bitesize videos.
The arrays you have seen so far are one-dimensional because they are a single list
stored in what looks like a single row table:
Position / Index 0 1 2 3
Value 10 20 30 40
Station Times
Edinburgh 07:30 12:30 15:30 18:30
Birmingham 10:15 15:15 18:15 21:15
Rugby 11:00 16:00 19:00 22:00
London 11:30 16:30 19:30 22:30
Position / Index 0 1 2 3
0 07:30 12:30 15:30 18:30
1 10:15 15:15 18:15 21:15
2 11:00 16:00 19:00 22:00
3 11:30 16:30 19:30 22:30
The departure time of the 2nd train from Edinburgh is held in position [0,1]. Always
read the row first and the column 2nd.
Note: Although we visualise the data as rows and columns, the computer does
not. We could visualise any of the tables above oriented as columns and
rows instead of rows and columns. So for the departure time of the 2nd train
from Edinburgh, it could also be held in position [1,0] depending on how the
list is structured.
© paullong.net 2020 Page 82 of 154 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 6 - Published by paullong.net
Notice how each row is included as a separate array with its own square brackets.
Wendy Davies has her marks stored in row 1. Her mark for test 4 is 27. This would
be referenced as position [1,4]. To display this, the following code would be used:
print(marks[1][4])
To enter a new mark for Jennifer Colthup (row 0) in test 2, the following code could
be used:
array_name[row,column]
row_length = len(array_name[row])
To calculate the number of rows within an array, use len for the whole array:
array_length = len(array_name)
2) Write code to display the following numbers from inside the array:
a) 34 b) 6 c) 22 d) 31 e) 13
4) Write code to ask the user to enter a row number and then a column
number. Then ask the user for a new value. Change the data in the position
chosen by the user to the value chosen by the user. For example, if the user
enters row 0, column 1 and a value of 99 then change 2 to 99.
A nested loop can be used to read or write all the data within a two-dimensional
array:
The outer loop loops through each row starting with row 0. The inner loop then
displays each data item in each column for the row. The outer loop then moves
onto row 1. This process continues until all rows have been displayed.
The code end="\t" means that it will separate each item with a tab instead of a
new line. Therefore, to start a new row for each student, a separate print
statement needs adding to print nothing to the screen.
The user could be asked to input marks for each student. First, the array needs to
be created:
Once the array has been created, the user can be asked for input of the marks for
each student’s test:
Run and investigate the example in the Python code file that uses the
list.append() method.
The number of arrays within the two-dimensional array may increase in the future
by using list functions in Python.
4) Find and display the index of the array that has the registration number
“RX82YHT”. Use a while loop.
5) Ask the user for a registration number and then display the data of the car
that has that registration number.
Records
Note: Python’s use of the term record is different to that used by Edexcel. Edexcel
use the term record to refer to a list which can have values of different data
types as opposed to arrays which can only have values of the same data
type
A record stores data about one thing but may have several items of data. For
example:
Index 0 1 2 3 4
0 Mitsubishi Outlander 2.0 Petrol True
1 Peugeot 305 1.4 Petrol False
2 Seat Ibiza 1.1 Petrol False
3 Ford Mondeo 1.6 Diesel False
A set of records is a two-dimensional list and so data can be accessed in the same
way as a two-dimensional list.
These could then be displayed to the screen using a nested for loop:
Predict what the output will look like for each car and then run the code to see if
your prediction was correct.
We could also display each car and refer to what each item means (its field
name):
Predict what the output will look like for each car and then run the code to see if
your prediction was correct.
Often records would look better if they were displayed in a tabular format. We
could use the format function and alignment feature to make the table look neat:
f = "{:<13}{:11}{:^14}{:8}{:7}"
print(f.format("Make", "Model", "Engine Size", "Fuel", "Hybrid"))
for car in cars:
print(f.format(car[0], car[1], car[2], car[3], str(car[4])))
#next car
This is what the output looks like when the code is run:
Why do you think the variable f was created? Modify the format code to see how
you could display the data differently. Try displaying the data in a different order
and try missing out some fields (columns).
You may need to search for records which meet certain criteria, for example all
the cars which have an engine size (position 2) bigger than 1.5 litres:
myFormat = "{:<13}{:11}{:^14}"
print(myFormat.format("Make", "Model", "Engine Size"))
for car in cars:
if car[2] > 1.5:
print(myFormat.format(car[0], car[1], car[2]))
#next car
Predict what the output will be for the code above and then run the code to see if
your prediction was correct.
1) Someone has attempted to display the data in a table but it has not worked
properly. Modify the code below to show the data in a neater table.
f = "{:<3}{:11}{:^14}{:8}{:7}"
print(f.format("Name", "Alias", "Height (m)",
"Number of Convictions", "Nationality"))
for c in criminals:
print("{:<10}{:14}{:^11}{:8}{:9}".format(c[0], c[1],
c[1], c[4], c[3]))
#next car
2) Write code to display just the names of all the criminals who are Scottish.
3) Write code to display the name and nationality of all the criminals with more
than 7 convictions.
Text files
Text files contain plain text and no formatting. They can be used to store data for
use later. In programming, data can be read from a text file and used by the
program, or data from the program can be written to a text file.
A plain text file consists of lines of text. Each line is separated by an end of line
character which is created by pressing the Enter key on the keyboard or by
inserting the ASCII characters 13 and 10 which represent carriage return (CR) and
line feed (LF).
carriage
Carriage return refers to the old days of typewriters
when the carriage would be returned to the
beginning of the line. In computing, carriage
return moves the cursor back to the beginning of
the current line. Line feed moves the cursor down
to the next line but in the same vertical position.
Therefore, the combination of both is to move the
cursor to the beginning of the next line.
Have you ever wondered why the Enter key uses this symbol?
CR
Normally you would not see the CR and LF codes in a text file.
Lines in a text file do not actually include line numbers as shown above, but a
programming language will refer to each line using a number. The numbering
system starts from 1 (one) rather than 0 (zero) which is a bit confusing when using
files with arrays.
Some text files might contain more than one item of data on each line. This is
often the case when storing files that contain records of data. Each item of data
needs to be separated using a delimiter. A delimiter is any character that is used
to specify where one item of data finishes and the next item of data starts. It
therefore marks the boundary between two data items.
The filename refers to the name of the file to open and myFile is the variable
that will refer to the file while it is open. As the file only needs to be read, and not
written to, it can be opened in read-only mode which is indicated by "r".
© paullong.net 2020 Page 91 of 154 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 6 - Published by paullong.net
Once the file is opened, its contents need reading. This can be done one line at a
time or all lines at once. To read a file one line at a time, use:
eachLine = myFile.readline()
If reading one line at a time, Python will read the contents as a single string. If
reading the whole file, then Python will read the contents as a list of strings which
each string being one line.
To display the first line of the file, use the following code:
plainFile = open("plain text.txt", "r")
firstLine = plainFile.readline()
print(firstLine)
plainFile.close()
This reads the first line from the file called “plain text.txt”, assigns it to a
variable called firstline and then displays it on the screen.
If the file is still open, then more lines can be read. Predict what you think the
output will be for the code below:
plainFile = open("plain text.txt", "r")
myLine = plainFile.readline()
print(plainFile.readline())
plainFile.close()
The first time readline() is used, it will read the first line. Each time after, it will
read the next line.
Did you notice in the code above that the file was closed when it was finished
with? This will be even more important when it comes to writing files, but it is
always good practice to close the file:
myFile.close()
myLines = myFile.readlines()
Notice that it was not necessary to use readlines(). This is because a file is also
an iterable. However, if you needed to use the data then it would need assigning
to an array or list. To do this, you could use:
Run the code and you will notice that it creates an array of strings with each line
being a separate string in the array. You could display each line in the array using
a for loop:
Each string contains an end of line character ("\n"). This can cause problems
when comparing data because "abc" is not the same as "abc\n". The end of
line character can be stripped from each line:
You read earlier about comma-separated value (CSV) files. These are very useful
because they store data in records and can be imported into a two-dimensional
list.
These methods above allow the data to be read, but not used properly or
manipulated. To use the data properly, it needs to be imported into a list. Python
includes a handy CSV library which can be imported. The code below opens the
campers file, recognises the files as comma-separated variables and then imports
the records into a two-dimensional list. Finally, it displays the data.
import csv
myFile = open("campers.csv", "r")
fileReader = csv.reader(myFile)
campers = list(fileReader)
myFile.close()
Did you notice that the data included the header row? This can sometimes be
helpful because it means the first record starts at index 1 instead of index 0.
However, it can also become a problem when manipulating the data.
Modify the code so that the header row is removed. Hint: the header row is index
0 and you could use the del function.
Now that you have imported the data into a two-dimensional list, you can display,
search and manipulate it just like you would any two-dimensional list.
Note: this does not stop you from writing code using the CSV library in an exam
unless the question tells you to not to use any libraries.
The code below will open the campers.csv file and display its contents to the
screen:
myFile = open("campers.csv", "r")
campers = myFile.read()
myFile.close()
print(campers)
Run the code to see if your prediction was correct. You were probably expecting
this to display:
2,Rachel,Larrison,18/07/1954,FALSE,4
but it actually displayed "m". The reason is that the read method above just read
the text as if it was a plain text file into a single string including the commas and
the line break characters. It has not separated this into an array with each line
being a string within the array and it has not separated the data by the commas.
Therefore, the character in index position 2 in this very long string is "m" from "Camper
ID".
One way of separating the single string into a list, is to split the string using '\n' as
the separator:
campers = campers.split('\n')
print(campers)
You will now see that each row is its own string in a list of strings. If we need to get
rid of a header row, then we can delete row 0:
del campers[0] # delete the first row (header row)
what the output will be if you now run the code below:
print(campers[0])
what the output will be if you now run the code below:
print(campers[0][1])
Run the code above to see if your prediction was correct. Were you expecting to
see "Coleman"? The reason you saw " ‘" was because although we have split
each row into an array of strings, it is still a one-dimensional list of strings. Each row
is still a string and not a list of values. The character in index position 1 is a comma.
What we can now do is separate each row into a list of strings for each field. We
can do this by working through each row one at a time using a for loop and
splitting the strings by commas:
for line in range(0,len(campers)):
campers[line] = campers[line].split(',')
#next line
print(campers)
Run the code above and you should see the campers as a two-dimensional list of
lists instead of a one-dimensional list of strings
This two-dimensional list can now be manipulated like any other two-dimensional
list.
Predict what the pupose is, and what the output will be, for the code below which
filters records:
for camper in campers:
if camper[5] == "3":
print(camper[2])
The example above uses the read method and splits into records by line breaks
and splits into fields by commas. You may also see a method that uses
readlines() which automatically reads the records into a one-dimensional list.
There will still be some work to do to strip the line breaks and split each record by
commas into fields to make a two-dimensional list.
del campers[0]
2) White some code to only display the campers who have a disability.
To write a string to a line of a text file, the code below could be used:
myFile.write(data)
In this code, filename is the name of the file and data is the data that will be
written to the file.
To save the message “Here is my text” to blank text.txt, use the following code:
The code above will overwrite any existing text. Modify the code so it writes
different text to the file and watch how the previous text has now disappeared.
The code below will append the data to an existing file so no data will be lost:
Try running the code above several times and watch what happens to the file
more text.txt.
myFile.writelines(list)
Although the name writelines suggests that this would write multiple lines, it just
joins together data in a list to write it to a single line in a text file. It is therefore
necessary to add the line separator symbol \n to each data item.
Now we can open a file in overwrite mode and save the names to the file as
separate lines:
myFile = open("blank text.txt", "w")
myFile.writelines(names)
myFile.close()
Open blank text.txt and check the data has been saved.
If you want to append multiple lines of data, then you need to open the file in
append mode. Modify your code so that it opens in append mode and check
that the names are being added to the file each time.
To add a new line of data, we will ask the user to input the data first:
forename = input("Please enter the camper's forename: ")
surname = input("Please enter the camper's surname: ")
dateofbirth = input("Please enter the date of birth: ")
Next we will prepare it in a format that includes the commas and line separator
symbol:
data_for_file = "41," + forename + "," + surname
+ "," + dateofbirth + "\n"
Open campers names.csv and check the data has been appended.
The example above works for a single line of text. If multiple lines were required,
then a for loop could be used to write several lines to the CSV file. However,
Python includes a function in the CSV library that takes away all the hard work.
To do this we can use the writerows function from the CSV library:
import csv
myFile = open("campers names.csv","a", newline='')
fileWriter = csv.writer(myFile)
fileWriter.writerows(campersList)
myFile.close()
Open campers names.csv and check the data has been appended.
© paullong.net 2020 Page 100 of 154 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 6 - Published by paullong.net
Note: The purpose of newline='' is to avoid having two carriage return (CR)
characters.
Questions – follow me
1) Arrays and records/lists are data structures that can be used in programming.
a) Describe the purpose of an array. [1]
b) Give one reason why lists are sometimes used instead of arrays. [1]
postcodes = ["W1 1AA", "W1 1AB", "W1 1AC", "W1 1AD", "W1 1AE"]
for position in range(0,5):
print(postcodes[position])
a) Explain why the range for the for loop finishes at 5. [3]
[ TURN OVER ]
4) A list of houses which include the house number, street name and postcode for
each house are stored in a two-dimensional array. A user can be asked for a
postcode, and then the house number and street of all houses with that
postcode will be displayed, e.g. 5a Turtle Grove.
e) If the user inputs “W1 1AA”, what will be displayed on the screen? [2]
[ TURN OVER ]
5) Students’ test results are stored in a 2D array. Part of the array for 4 of the tests
is shown below:
Truth Tables 45 34 46 49 30 31
Networks 42 29 51 35 31 41
For example, Caleb scored 48 in the binary test. There are no half marks.
b) i) Identify the data type that should be used to store the scores in this
array. [1]
ii) State why this data type is the most appropriate. [1]
c) The code to display the score for Leo in the logic gates test is:
print(scores[3][1])
(i) Write the code to output the score for Kim in the Truth Tables test. [1]
print(scores[4][3]) [1]
(iii) Write an algorithm to calculate the total of all the scores for Naomi.
You must not use any built-in statistical methods or functions. [2]
d) Write an algorithm to display the average score for all students in all tests. [5]
Each of these stages will involve various steps. For example, adding ingredients to
the cup could include:
a) Add coffee
b) Add sugar
c) Add water
d) Add sugar
Breaking the problem down into groups of steps is known as decomposition. Each
group of steps can then be run together. It is a bit like following a user guide
where instructions are grouped into sections so that you can follow just the
instructions needed.
It is called by writing the name of the subroutine into the programming code,
together with any parameters:
sub_name(parameters)
When a subroutine has finished executing its code, the program will continue with
the next line of programming code after the subroutine was called:
# programming code run before the subroutine
sub_name()
# programming code run after the subroutine
Procedures
Procedures and functions are both types of subroutines. Procedures do not return
any data to the programming code but simply run a sequence of instructions. It
helps when following code to prefix subroutines, for example proc_ for procedures.
Example – procedures
Within a program, there are several occasions when a company’s name and
address need to be displayed on the screen. A procedure can be used to
perform this task:
def proc_display_address():
print("Microsoft UK")
print("Microsoft Campus")
print("Thames Valley Park")
print("Reading")
print("RG6 1WG")
#end proc_display_address
Some subroutines are pre-defined within a programming language and are known
as built-in subroutines. An example would be print() which displays data to the
screen. Subroutines that are defined by the user are called custom-made
subroutines.
A subroutine is like a black box – what happens inside is not seen by the rest of the
program. The task is performed and then the program carries on.
Activity – procedures
1) Create a subroutine to ask the user for their name and then to display on the
screen a welcome message to the user.
2) State the identifier you would use to call the subroutine.
print("Menu")
print("1 – Cash withdrawal")
print("2 – Balance")
print("3 – PIN Services")
print("4 – Pay bills")
print("5 – Cancel")
user_option = input("Please select an option 1-5: ")
if user_option == "1":
proc_withdrawal()
elif user_option == "2": Predict what will happen if the user inputs 1.
proc_balance()
elif user_option == "3":
proc_pin() Experiment by running the code and
elif user_option == "4": investigating what other options do.
proc_bills()
Try inputting a value that is not 1, 2, 3, 4 or 5.
elif user_option == "5":
proc_cancel()
else:
print("Please only enter an option 1, 2, 3, 4 or 5")
#endif
ATM
Parameters
Subroutines can become more useful when they can be used for different values
of data. To do this, data has to be passed into the subroutine. This is done using
parameters.
Example – parameters
The procedure below states how many siblings there are:
In the example above, the parameters in the main program were bro and sis
which were passed through as brothers and sisters in the subroutine.
Note: Arguments are the actual values that are passed. If the user had input 5 for
bro and 2 for sis then the arguments being passed would have been 5
and 2. In an Edexcel exam you only need to be aware of the term
parameters which will be used to refer to both parameters and arguments.
Activity – parameters
Create a subroutine that displays the name and age of a person on the screen. If
the person is under 18 then they are told they are “too young”.
def sub_name(parameters)
# programming code goes here
return result
length = len(myString)
Example – functions
This function calculates the perimeter of a rectangle:
length = 5
width = 7
perimeter = func_perimeter(length, width)
def func_array_last(array_name):
last_array_position = len(array_name) - 1
return last_array_position
#end array_last
scores = [5,8,2,1,9,10,7,6]
last_position = func_array_last(scores)
So far, the examples you have seen are quite simple and only include one or two
lines of code for each function. Functions become particularly useful when they
include several of lines of code and can then be used to represent all those lines
of code in a single word.
def func_array_total(array_name):
array_total = 0
for counter in range(0, func_array_last(array_name) + 1):
array_total += array_name[counter]
#next counter
return array_total
#end func_array_total
def func_array_minimum(array_name):
array_min = array_name[0]
for counter in range(0, func_array_last(array_name) + 1):
if array_name[counter] < array_min:
array_min = array_name[counter]
#endif
#next counter
return array_min
#end func_array_minimum
total_score = func_array_total(scores)
min_score = func_array_minimum(scores)
Python does already have built-in methods for calculating the total and minimum
values in an array, but these are examples of how they might be implemented.
Activity – functions
1) Create functions (subroutines with return values) for the following:
a) Calculate the area of a circle (use π = 3.14).
b) Calculate the average value in an array.
c) Calculate the maximum value in an array.
middle = slice_mid("computer", 2, 4)
if the code below will give the same result? Explain why.
middle = "computer"[2:4]
Investigate how the code within the function works. Annotate each line of code
to explain what it does.
Scope of variables
Scope is the extent to which something is relevant. Therefore, the scope of
variables is the extent to which variables are relevant.
When you write a program, any variables declared within the main part of the
program are known as global variables. This means that the variable is available
to every part of the program. The scope is global.
Note: it is also possible to have variables whose scope is within a module and are
available to any functions within that module, but not available outside of
that module.
Example – scope
The function below uses two local variables (var1 and var2) and returns the new
value of var2 to the main program:
The code below calls the my_sub function and passes the values 4 and 5 so that
var1 in my_sub starts as 4 and var2 in my_sub starts as 5.
var1 = 2
var2 = 3
var3 = my_sub(4, 5)
var4 = var1 + var2 + var3
Run the code to see if your prediction was correct. Experiment with the code to
investigate what is happening at each stage. It will help you to know that the
orange variables above are local variables and the red variables above are
global variables.
Example – pancakes
Imagine you are going to make some pancakes for Shrove Tuesday. You could
start by separating the problem into 3 main areas:
1) Organise kitchen
2) Make pancakes
3) Serve pancakes
4) Wash up
Pancakes for
Shrove
Tuesday
Each smaller problem can now be dealt with separately. This means that one
person could organise the kitchen and a different person could do the washing
up.
Each of these smaller problems can be broken down into even smaller problems.
Let’s consider organising the kitchen:
Organise
kitchen
Now we have even smaller problems. Each of these problems can again be dealt
with separately and so are much easier to solve. The whole problem starts to look
like this:
Clean surfaces
Whisk
Add butter
Get pan to
temperature
Whisk
Pour batter in
Cook
Spread batter to
edges
Fill bowl with
Get crockery
water
Flip
Put utensils in
Get cutlery
water
Serve pancakes
Serve pancake to
plates
Wash The “Cook” problem has
been broken down into tiny
Put crockery in
Wash up
water sub-problems.
Wash
Put cutlery in
water
Wash
Advantages of subroutines
Main
program
Questions – follow me
x = 5
y = 6
z = sub_area(x, y)
def sub_c(cash):
x = 0
for i in range(1,len(cash)):
x = x + cash[i]
return x
[TURN OVER]
def sub_p(x):
z = x[-1]
return z
y = sub_p("Hello")
Till
Lookup
Calculate
Scan barcodes products and Calculate total
change
prices
The developer is taking a structured approach to developing the solution to
this problem. He wants to implement each sub-problem as a subroutine. Each
subroutine will have its own interface.
The subroutine for calculating the total will include a parameter to receive an
array of the prices that were found when looking up the products and prices.
The subroutine must return the value of the total of all the prices.
9) Turtle
Turtle is a Python library that can be used to draw on the screen. The library must
be imported using:
import turtle
Setting positions
The following methods can be used to change the position of the pen:
The coordinates 0,0 are in the centre of the Python screen. The x coordinates go
across the screen with positive values to the right and negative values to the left.
The y coordinates go vertically with positive values going up and negative values
going down.
y
(-x,y) (x,y)
-x (0,0) x
(300,-75)
(-x,-y) (x,-y)
-y
turtle.setpos(300, -75)
for line in range(0,3):
turtle.forward(100)
turtle.left(120)
#next line
turtle.home()
turtle.backward(100)
turtle.reset()
Turtle properties
The following methods can be used to change the properties of the pen:
turtle.pensize(20)
turtle.penup()
turtle.setpos(-200, 200)
turtle.pendown()
for line in range(0,5):
turtle.forward(100)
turtle.left(72)
#next line
turtle.penup()
turtle.setpos(200,200)
turtle.pencolor("magenta")
turtle.pendown()
turtle.forward(100)
turtle.hideturtle()
Questions – follow me
import turtle
turtle.forward(50)
turtle.right(60)
turtle.forward(50)
turtle.right(60)
turtle.forward(50)
turtle.right(60)
turtle.forward(50)
turtle.right(60)
turtle.forward(50)
turtle.right(60)
turtle.forward(50)
turtle.right(60)
a) Modify the code so it uses a for loop to reduce the number of lines of
code. [2]
b) Modify the for loop you have created so it creates a square with red lines
and a line width of 10. [3]
2) a) Write code to ask the user for the number of sides, colour and pen width
to use to draw a polygon. The code should then draw the polygon. [8]
c) Test your code by calling the procedure with the following line of code:
[ TURN OVER ]
3) Write code to draw a square house with a triangular roof, 3 square windows
and a rectangular door. Your code should reuse a single procedure for
drawing each window and the house. You should also create a procedure to
draw the roof and another to draw the door. Do not use Python’s built-in turtle
shapes. An example of what your house might look like is shown below:
You should use no more than 15 lines of code in the main program, and each
procedure should have no more than 8 lines of code. [10]
Once a program has been written and tested, it will be used by the users. During
this time, it may be necessary for maintenance to take place on the code.
Maintenance might be necessary because:
• errors have been found
• a security loophole has been found
• the customer would like some improvements to be made
• the way a business works changes meaning the program needs to change
The key point is that the programmer decides how to name the identifier. It is very
important that the programmer chooses identifier names that are meaningful and
can be understood throughout the program. For example, using t for telephone
number is not very helpful, but TelNo can be understood by the programmer and
anyone else using the code. There are no golden rules for how to name identifiers
and many languages have their own conventions, but these are some general
guidelines you could follow for good practice:
When you want to use more than one word for an identifier, you could separate it
with the underscore (_). For example, house_number or end_of_file.
Some programmers use what is known as camel case to separate words. Instead
of using an _ to separate words, they use a capital letter at the beginning of each
new word (except the first word). For example, houseNumber or endOfFile. This is
only possible in languages that allow capital letters. Different languages tend to
have their own conventions. JavaScript programmers tend to use camelCase
whereas PHP programmers tend to use the underscore.
© paullong.net 2020 Page 124 of 154 by Paul Long
The Ultimate GCSE CS Textbook for Edexcel - Chapter 6 - Published by paullong.net
Compare the two algorithms below and predict what each does. Start with the
first algorithm before you look at the second one.
1 print("Hello")
2 x = input("What is your name? ")
3 y = input("How old are you? ")
4 z = yb(y)
5 print(x + ", you were born in " + z)
1 print("Hello")
2 yourname = input("What is your name? ")
3 age = int(input("How old are you? "))
4 YearOfBirth = calculateYearOfBirth(age)
5 print(yourname + ", you were born in " + YearOfBirth)
The first algorithm is difficult to follow because it is unclear what the purpose of the
function db is and line 5 uses identifier names that do not mean anything to the
programmer.
The second algorithm is much easier to follow as the function is described clearly
using the identifier name and the variables are also clearly identified. Note that
using abstraction, it is not necessary to know how the calculateYearOfBirth
function works.
Comments
When writing code, it is good practice to add comments to explain what each
part of the code does. This makes it easier for the programmer to understand
what they wrote and why, but also for a future programmer who makes changes
to the code to understand what the program does. It is also good practice to
explain any variables, parameters and return values that are being used.
// Its purpose is to process any form and append the form's data
// to a file
// The file is determined by a field (can be hidden) in the form
// called "filename"
// Other fields should be named appropriately for the purpose of
// the confirmation // screen
The comments help to understand what the code is doing at each stage. Here is
the code without comments – it is much more difficult to work out what is going
on:
$filename = $_POST['filename'];
Comments can also be used to temporarily disable a line of code by putting the
comment symbol at the start of the line.
Use of constants
If a value does not need to change when the programming code is run, then it
can be declared as a constant. For example, the speed of light, π (Pi),
In the first algorithm, it may not be clear what 3.14 means, but in the second
algorithm PI is clearly being used.
These algorithms both calculate the time it takes to see light from Mars:
1 SPEED_OF_LIGHT = 299792
2 AVG_DISTANCE_TO_MARS = 225000000
3 timeToSeeLightFromMars = AVG_DISTANCE_TO_MARS / SPEED_OF_LIGHT
In the first algorithm, it is not clear what the numbers mean in the calculation, but
by declaring them as constants in the second algorithm, it is clear what the
calculation is doing.
Indented code
You may have noticed that gaps have been placed at the left-hand side of
blocks of code. These gaps are known as indents. They help to show the
programmer where a block of code finishes and where it ends. This is important for
debugging and understanding how the code works.
The arrows show how the code has been indented to make it easier to follow. The
end of each for block is lined up with the start of the for block it belongs to.
The text to display to the screen is too long to fit on one line so it has been split and
indented to keep it together.
description = "Toddler"
else:
description = "Baby"
#endif
#endif
print("The person is:", description)
Activity – indentation
Find some code you have already written.
White space
When you write a story or an essay, you use paragraphs to separate out sections of
your writing. The same can be done with code. Leaving a line gap between
sections of code makes it more readable.
Notice how line gaps (white space) have been left between the input section, the
calculation section and the output section.
Subroutines
You learned about subroutines earlier in this chapter. Using subroutines makes
code more readable because the detail of each subroutine can be ignored when
looking at the whole program. Subroutines also make the code more
maintainable because each subroutine can be tested individually so it is easier to
find errors.
Many professional programmers will never write more than about 4 or 5 lines of
code at a time. Instead, they will write subroutines which complete small tasks
Example – subroutines
Examine the code below:
usersFileName = "users.csv"
usersList = importUsers(usersFileName)
successfulLogin = False
while not successfulLogin:
username = requestUsername()
password = requestPassword()
successfulLogin = loginUser(username, password, usersList)
#end while
displayMenu()
followMenuOption()
The code uses 6 subroutines. Each subroutine performs a small task. By using
meaningful identifier names for the subroutines, the code is more like pseudo-code
and is very easy to follow. Notice also how white space, indentation and other
meaningful identifier names make the code easy to read. Comments have not
been necessary in this code because it is so easy to understand.
Questions – follow me
1) Describe three factors you should consider when deciding on an identifier
name. [3]
2) One way of making code maintainable is to use subroutines. State three other
ways in which code can be made maintainable. [3]
4) Examine the code below which gives 2 points if the answer to a question is
correct and 1 point if the answer to a question is incorrect.
def qu(s):
x = input("What is the Capital of France? ")
if x == "Paris":
print("Well done. You get 2 points")
s = s + 2
else:
x = input("Try again: ")
if x == "Paris":
print("Well done. You get 1 point.")
s = s + 1
else:
print("Oops. Wrong answer. You get 0 points.")
return s
total = qu(5)
print("Your total is:", total)
Rewrite the code so it is more maintainable. You do not need to use any other
subroutines. [7]
Validation routines
Activity - valid
If you were to look up “valid” in a thesaurus, what words would you find?
Spend 1 minute on your own listing similar words.
Now spend 1 more minute with a partner comparing your list and discovering
more words.
Now spend 2 minutes with 3 different people (i.e. group of 4) sharing your list and
discovering new words.
Discuss your findings with your class teacher.
If something is valid, it means that it meets the allowed criteria. A credit card has a
valid from date and a valid to date. That means it is acceptable to use it within
that range of dates. One of the words you may have come up with as a similar
word to valid could be “correct”. However, as you will see later, if data is valid it
does not mean that it is correct.
Activity – validation
Copy the database Cleaners.mdb and open it – a form should open.
1) Enter your forename, but leave the surname blank. What happens when
you try to save the record?
2) Now put in your surname, but put tomorrow’s date as your date of birth.
What happens when you try to save the record?
3) Put your date of birth in and enter Y for your gender. What happens when
you try to save the record?
4) Put your gender in and try to enter 90 years of service. What happens when
you try to save the record?
5) Try “d” as the number of years’ service. What happens when you try to save
the record?
6) Put 2 as the number of years’ service and input the NI number as YT821092E.
What happens when you try to save the record?
There are several different methods that can be used to check data when it is
input. The most common of these are explained below.
Presence check
A presence check simply ensures that some data has been entered for a specific
field. In other words, the field cannot be left empty.
Long = valid
L = valid
[null] = invalid (null means blank)
In programming code, you can check if the user has entered any data. If they
have not, then they can be presented with an error message and asked to enter
the data.
surname = ""
while surname == "":
surname = input("Please enter your surname: ")
#endwhile
The code above did not include an error message. Run the code below to see
how it is easier for the user to understand their mistake:
Range check
A range check will ensure that data is within an acceptable range. There will be a
lower limit and a higher limit. When you followed through the validation activity,
you attempted to enter a number (90) that was not within the acceptable range
that had been set for the number of years’ service field. The range started at 0
and finished at 25. Any data outside of the range is invalid and will be rejected.
Predict what the output will be when the following values are input using the code
above:
-3 0 1 9 24 25 26 80
Run the code and input the values above to see if your prediction was correct.
2) Write a validation routine using code to check the input of a person’s age is
at least 18.
Length check
A length check ensures that data is a minimum, maximum or specific length. Only
data that meets the length requirements will be accepted.
This validation rule ensures a telephone number is at least 6 digits long and a
maximum of 15 digits long:
Experiment with different lengths of surname and telephone number. Try to see if
you can find the shortest and longest surnames and telephone numbers that can
be input.
Type check
A type check will check to see that the data is the correct data type. Only data
of the specified data type will be accepted. This would usually be tested using a
function. In Python, these functions include:
Each function returns a value of True if the criteria are met and False if the
criteria are not met.
The difference between isdigit and isnumeric is very subtle. isdigit will
return True if only digits 0-9 are in the value. isnumeric will also return True for
digits 0-9 but will also return True for characters from other languages such as
Chinese and for Unicode fractions, subscript numbers and superscript numbers.
isalpha will also return True for foreign alphabetical letters.
When you followed through the Validation Activity and tried to enter “d” as the
number of years’ service, it was rejected because it was not an integer data type.
This validation routine deliberately receives the price from the keyboard as a
string. If it tried to receive the price as an integer and a string was input it would
crash the program rather than giving a helpful error message.
Pattern check
A pattern check will ensure that data entered matches a specific format. When
you tried to enter different types of National Insurance numbers in the cleaners
database, it would only accept data that matched the following pattern check:
The type check functions can be used to test individual characters within an input
string.
valid = False
while not valid:
product = input("Please enter product code: ")
Experiment with different values for input, including both uppercase and
lowercase characters.
1) Using the pattern check in the example, which of the following product
codes are valid and which are invalid?
b) Which of the following data items are valid for the routine above?
B4 4B B44 44B BB44 44BB b4 4b b44 44bb
3) Write a validation routine using code to check that a single lowercase letter
has been input.
Lookup check
A lookup validation checks to see if the data entered exists in a list.
grades_list = ["A","B","C","D","E","U"]
grade = input("Please enter grade: ")
while grade not in grades_list:
print("Grade must be A-E or U")
grade = input("Please enter a grade A-E or U: ")
#endwhile
Experiment with different values for input, including both uppercase and
lowercase characters.
The example above uses data that is defined within the code. It is also possible to
check a list that is stored within a file, but that is beyond GCSE level. If you want a
challenge though, you could try writing a subroutine that checks if data input exists
within a list stored in a file.
2) Adapt the validation routine above to check the input of a person’s gender
is M or F. The code should accept accidental lowercase input and change
it to uppercase.
3) Write a validation routine to check the input of a person’s title is Mr, Mrs, Miss
or Ms.
Accuracy
The purpose of each validation method described above is to reduce input errors
by identifying invalid data at the point of data entry. This means that when data is
input, if it is invalid then it will be rejected. However, this only ensures that valid
(reasonable) data is accepted and invalid data is rejected. What happens
though if valid data that is incorrect is accepted?
Example - accuracy
In this example, only “M” for male or “F” for female are allowed to be entered. This
is what happens when the following data is entered:
But Alex is not female. So why has the data been accepted? The reason is
because the data is valid. The system is only checking that the data matches the
rules. It is therefore possible that data that matches the rules, but is incorrect, can
be accepted. This means that it is not possible to use validation to ensure that
data is accurate and so there is no certainty of accuracy.
Authentication routines
Authentication is the process of checking that a user is who they say they are. I
could turn up to a computer and pretend to be Theresa May and if no password
or other authentication method is required then my logon will be accepted.
authenticated = False
while not authenticated:
username = input("Enter username: ")
password = input("Enter password: ")
if username == "LongPA" and password == "letmein":
authenticated = True
print("Welcome.")
else:
print("Username and password not recognised.")
#endif
#endwhile
Usernames and passwords are usually stored in an encrypted file. For the purposes
of GCSE, we will store a username and password in a plain text file.
In reality, there will be more than one username and each user will have their own
passwords.
authenticated = False
Questions – follow me
1) Identify, describe and give an example of a validation rule that could be used
on the field “Transmission” for a vehicle which can be “Manual” or
“Automatic”. [3]
2) Identify, describe and give examples of two different types of validation rules
that could be performed on GCSE Grade (1-9). [6]
State the type of validation check being performed by this routine. [1]
[TURN OVER]
authenticated = [A]
while [B]:
[C] = input("Enter username: ")
password = input("Enter password: ")
if username == "LongPA" and [D] == "letmein":
authenticated = [E]
print("Welcome.")
else:
print("Username and password not recognised. Try
again.")
7) Write a validation routine using code that checks the presence of a postcode.
If there is no postcode, an error message should be displayed and the user
should be asked to enter it again until one is entered. [5]
8) A National Insurance number must consist of two upper case letters followed
by six numbers followed by one letter between A and D. Complete the code
below to check a National Insurance number is valid. [8]
def checkNI(_________):
valid = _________
length = len(niNumber)
if ______ == 9:
if niNumber[0:___].isalpha():
if niNumber[-1].upper() in ["A","B","C","D"]:
if niNumber[____:____].isdigit():
valid = True
return ______
12) Errors
An error in a program is often called a Bug. Bugs cause a program not to run as it
was intended. A bug is usually an error in the programming code.
There is a myth that Grace Hopper first used the term ‘computer bug’
when she discovered a moth in a Harvard University calculator in
1945. It is true that she found a moth, but it was not the first time the
word ‘bug’ had been used for an error. The word ‘bug’ referring to
faults can be traced back as far as Thomas Edison in 1878. Thomas
Edison invented the light bulb.
1 print("Welcome"
2 name = input(Please enter your name: )
3 age = input("Hello + name + how old are you?")
4 newage = age + 10
5 print("In 10 years you will be", age "years old)
6 while age < 100
7 age = age + 1
8 year = year + 1
9 #endwhile
10 print("You will be age 100 in", years, "time)
Identify the errors within the code. Check your errors with another student.
Video - debugging
Watch http://tiny.cc/bbcdebugging from 1:39 to 3:46 about how a tester finds
errors in computer games.
Syntax errors
Syntax refers to the rules a programming language. Computers can only follow
the instructions given. If the instructions are spelt incorrectly or don’t follow the
rules of the programming language, then the code cannot be understood by the
computer.
• i instead of I
• ? instead of , (comma)
• “be” instead of “to be”
• computor instead of computer
• ; (semi-colon) instead of . (full stop)
Logic errors
Logic errors cause a program to give unexpected results. The code can be
understood and can be run, but the output is not what was expected.
To calculate how much travel expenses and employee should be paid, the
number of miles travelled should be multiplied by the mileage rate. Examine the
code below and predict what the value of expenses will be:
endMileage = 100
startMileage = 90
mileageRate = 0.45
expenses = endMileage - startMileage * mileageRate
This will not stop the program from running. However, the answer it gives will not be
what is expected. It will actually calculate:
Run the code to see how different answers would be given for each attempt.
what will be the value of status for someone aged exactly 18?
This will give incorrect results. If somebody is aged 18, they will not be classified as
an adult. This should have been written as:
while True:
temperature = int(input("What is the temperature? "))
if temperature == 20:
heater = "On"
else:
heater = "Off"
#endif
print(heater)
time.sleep(30)
#endwhile
In all the examples of logic errors above, the program would still run, but it would
give unexpected results.
Runtime errors
A runtime error causes a program to crash when it is running. This can happen if a
loop runs infinitely.
A runtime error could also cause a program to run out of memory. If a recursive
algorithm never stops, then the computer will eventually run out of memory.
def multiply(x):
x = x * 5
print(x)
multiply(x)
#end multiply
Testing is necessary to find logic errors and runtime errors. By selecting appropriate
test data and running a range of tests, it is likely that logic errors and runtime errors
will be found. However, some errors may not be found until the program has been
operational for some time and so software patches are needed to fix them.
Identify which errors are logic errors and which errors are syntax
errors.
Note: The incorrect use of age as both a string and integer would be classed by
some scholars as a syntax error and others as a logic error and others as a
runtime error. Similarly for the use of years instead of year. Some scholars
say that a logic error will never give an error message during run-time and
only runtime errors will allow a program to compile and run, but generate an
error during run-time.
It will run successfully to start with and the user is asked to input a number:
However, because Python accepts user input as a string, a runtime error occurs
when the integer (6) is added to the string (“5”).
x = 6
y = int(input("Please input a number: "))
z = x + y
print("Total =", z)
Activity – debugging
Examine the common error messages presented by Outwood Academies Trust at
http://tiny.cc/debugpython
Using a program you have written before, introduce an error into it. Try to predict
which error message will appear. Repeat this for different errors.
Some IDEs will report errors at the time of writing the code. It’s worth using a good
IDE for programming because they will include features such as:
Questions – follow me
1) An error is detected while a program is being executed.
a) Name the type of error that causes an error message while a program is
being executed. [1]
b) Name a type of error that prevents a program from being executed. [1]
2) A credit card can be used to pay for goods and services. It will have a limit
which cannot be exceeded. For example, if a person has a limit of £7,000 and
has already spent a balance of £6,900, then they can only spend another £100
before they reach their limit.
The function below is used when an attempt is made to pay by credit card:
1 def checklimit_correct(balance,payment,limit):
2 if balance + payment <= limit:
3 return "Payment confirmed"
4 else:
5 return "Payment declined"
The algorithm contains two errors. Identify the line number of each error, give
the type of error and show the corrected code for that line. [6]
The programmer tests the code and inputs the value 2 for x and 3 for y. The
programmer is expecting the value of answer to be 8.
a) State the actual result for answer when the code is run. [1]
b) Explain why the actual result does not match the expected result. [2]
c) State the type of error that has caused this. [1]