0% found this document useful (0 votes)
88 views11 pages

Copy of Copy of T6 Worksheet 6

The document provides details for programming tasks including: 1) A program that asks a user to enter their name, last name, and age between 1-20 and outputs a greeting with their name length and next year's age. 2) The program then lists the birthdays the user has had based on their current age input.

Uploaded by

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

Copy of Copy of T6 Worksheet 6

The document provides details for programming tasks including: 1) A program that asks a user to enter their name, last name, and age between 1-20 and outputs a greeting with their name length and next year's age. 2) The program then lists the birthdays the user has had based on their current age input.

Uploaded by

jabeve5362
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Worksheet 6 Errors and testing

Unit 6 Programming

Name: Class:

Task 1
Edward has drawn a flowchart to show the basic steps in a program for a game. A player
continues to roll two die, adding the numbers rolled to his score. The player can stop at any
time, but if he rolls a double before stopping his score goes back to zero.

(a) Use the flowchart to write a program from this algorithm.

1
Worksheet 6 Errors and testing
Unit 6 Programming

import random
answer = "y"
die1 = 0
die2 = 0
score = int(0)
while answer == "y":
die1 = random.randint(0,6)
die2 = random.randint(0,6)
if die1 == die2:
print("You rolled the same number," , die1 , "score is set to 0")
score == 0
else:
score = score + die1 + die2
print("You rolled" , die1 , "and" , die2, "your current score is" , score)
answer=str(input("Do you want to go again? Y/n"))
print("Your score is" , score )

2
Worksheet 6 Errors and testing
Unit 6 Programming

(a) Write down up to three examples of syntax errors that you made in your program as you
developed it.
1) didn’t add import random
2) die1 = random.randint(0,6) i didn’t add randint
3) forgot a speech mark

(b) Correct these errors and make sure your program works correctly.
(c) Insert three deliberate syntax errors in your program. Write them down below.
while answer == y":

if die1 = die2:

score = score + die1 - die2

(d) Now insert a logic error in the program. Test it yourself to see the effect. Write the error and
the expected result below. Swap with a partner and ask them to find and correct the error.
die1 = random.randint(0,8)

(f) Write down the logic error that your partner added to their program.

die2= random.randint(0,8)

3
Worksheet 6 Errors and testing
Unit 6 Programming

Task 2
The program shown below is intended to model the calculation of the toll charge for crossing a
bridge on a motorway. Between 6am (0600hrs) and 10pm (2200 hrs) inclusive, the charges are
as follows:

● Motorcycle £1.00 (Vehicle type 1)

● Car £2.00 (Vehicle type 2)

● Goods vehicle £2.50 (Vehicle type 3)

● HGV and coaches £5.00 (Vehicle type 4)

After 10pm and up to but not including 6am, there is no charge.


Amanda writes the following pseudocode:
time = input("Please enter current time (24 hour clock): ")
if time < "0600" or time > "2200":
print("charge = 0.00")
else:
print("Vehicle types:")
print("Motorcycle = type 1")
print("Car = type 2")
print("Goods vehicle = type 3")
print("HGV and coaches = type 4")
vehicleType = input("Please enter vehicle type (1, 2, 3 or 4: ")
if vehicleType == "1":
print("charge = 1.00")
elif vehicleType == "2":
print("charge = 2.00")
elif vehicleType == "3":
print("charge = 2.50")
elif vehicleType == "4":
print("charge = 5.00")
else:
print("Invalid vehicle type")
print(charge)
Complete the following test plan.

Data Expected
Test Data Actual
(Vehicle Reason for test result for
number (Time) result
type) Charge
1 1 1245 Valid time 1.00 1.00
not
2 2 2500 logic error 0.00
possible
3 4 1530 Valid time 2.50 2.50
4
5

4
Worksheet 6 Errors and testing
Unit 6 Programming

5
Worksheet 6 Errors and testing
Unit 6 Programming

(a) Are your tests sufficient to thoroughly test the program? What assumptions have
been made?
all values above 2200 have

(b) Is the logic of the program correct? If not, what should be changed?
she should define charge

Task 3
The following program performs a binary search to find an item in a sorted array A of length n. If
the item is found, its position in the array is displayed. If the item is not in the list, “Item not
found” is displayed. Assume that the first element of the array is A[0].

itemFound = False
searchFailed = False
names = ["Anna", "Bill", "David", "Faisal", "Jasmine", "Jumal", "Ken",
"Michela", "Pavel", "Rosa", "Stepan", "Tom", "Zac"]
itemSought = input("Search term: ")
first = 0
last = len(names)-1
while not (itemFound or searchFailed):
print(first,last)
midpoint = int((first + last)/2)
if names[midpoint] == itemSought:
itemFound = True
else:
if first > last:
searchFailed = True
else:
if names[midpoint] < itemSought:
first = midpoint + 1
else:
last = midpoint - 1

if itemFound:
print("Item is at position ", midpoint)
else:
print("Item is not in the array")

6
Worksheet 6 Errors and testing
Unit 6 Programming

(a) The user searches for the name Tom. Complete the trace table below

itemFound searchFailed first last midpoint names[midpoint] OUTPUT

Tom no 0 12 6 ken

no

(b) The user searches for the name Erik. Complete the trace table below

itemFound searchFailed first last midpoint A[midpoint] OUTPUT

no no 0 12 6 ken

no no 0 5 3 david

no no 3 5 4 faisel

no no 3 3 3 david

no no 3 2 3 david

item is not in
no yes / / / /
the array

7
Worksheet 6 Errors and testing
Unit 6 Programming

Task 4
Three lists store the names, ages and year group of five students. A programmer wishes to
combine these into a 2D list (a list of records). They write the following code:
names = ["Anna", "Bill", "Faisal", "Pavel", "Tom"]
ages = [14, 16, 14, 17, 15]
year = [10, 11, 9, 13, 10]

#Algorithm 1
records = []
for i in range(len(names)):
records.append([names[i], ages[i], year[i]])
print(records)

#Algorithm 2
namesAges = []
allDetails = []
for i in range(len(names)):
namesAges.append([names[i], ages[i]])
for i in range(len(names)):
namesAgesRecord = namesAges[i]
namesAgesRecord.append(year[i])
allDetails.append(namesAgesRecord)
print(allDetails)

Both algorithms achieve the correct output, however, they do it differently.


(a) How many iterations will algorithm 1 take to create the list records?

(b) How many total iterations will algorithm 2 take to create the list allDetails?

(c) Which algorithm is more efficient in the number of iterations required?

8
Worksheet 6 Errors and testing
Unit 6 Programming

(d) Which algorithm is makes a more efficient use of memory?

9
Worksheet 6 Errors and testing
Unit 6 Programming

Paper 2 Example Question 6

Suggested time: 20 minutes

A program is needed that meets the following requirements:

● Ask the user to enter their first name, last name and age

● Accept ages from 1 to 20 inclusive

● Output the text:

● Hi <firstname>. Your full name is <length> characters long and


you will be <age> next year.

Where <firstname> is the name they typed in, <length> is the total length of their
first and last name and <age> is the age they will be in a year’s time.

● Then print each of the birthdays they have had. For example, if they are age 3,
print:
You have had the following birthdays:
First birthday
Second birthday
Third birthday

Open file Q06.

Amend the code to:

● Fix the logical error in the ORD_NUMS constant

● Fix the syntax error in line 21

● Fix the runtime error caused by lines 27 and 29

● Fix the logical error in line 34

● Create the correct output statement, as given above, in lines 39 and 40

● Create code to output each of the birthdays that the user has had

Do not add any additional functionality.


Save your amended code file as Q05FINISHED.py
10
Worksheet 6 Errors and testing
Unit 6 Programming

(Total for Question 5 = 14 marks)

11

You might also like