Copy of Copy of T6 Worksheet 6
Copy of Copy of T6 Worksheet 6
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.
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:
(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:
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
Tom no 0 12 6 ken
no
(b) The user searches for the name Erik. Complete the trace table below
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)
(b) How many total iterations will algorithm 2 take to create the list allDetails?
8
Worksheet 6 Errors and testing
Unit 6 Programming
9
Worksheet 6 Errors and testing
Unit 6 Programming
● Ask the user to enter their first name, last name and age
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
● Create code to output each of the birthdays that the user has had
11