Python Programming: Unit Exam B
Functions and procedures, 2D lists, dictionaries, files and append mode, scope errors, choosing a data structure, and a program to complete.
Instructions
- Time allowed
- 25 minutes
- Total marks
- 25
- Calculator
- No calculator is permitted.
- Answer all the questions.
- The number of marks for each question is shown in brackets. Use it as a guide to how much to write.
- The clock at the top counts down from the real time allowed. When it reaches zero the paper is locked and marked, as it would be collected in the exam hall.
- Read every question to the end before answering. Where a question says tick two boxes, tick exactly two: ticking more than asked scores nothing. Type one word and number answers carefully, because they are marked exactly.
Questions
Answer every question, then finish the paper to have it marked. Marking as you go teaches you far less.
def area(r):
return 3 * r * r
print(area(2) + area(1))- L15
area(2) is 12 and area(1) is 3: 15.
Tick one box.
- LC def show_menu(): print("1. Play")
show_menu does a job and returns nothing, so it is a procedure.
- LB1
- LC2
- L2
Row 1 is the B row and row 2 the C row. Each row has 2 seats.
- LMadrid
- L3
The value for Spain is Madrid, and adding Italy makes three entries.
scores = {"Ana": 72, "Ben": 45, "Cara": 50, "Dev": 38}
# print the name of every student with a score of 50 or more
- LUses a loop over the dictionary
- LPrints Ana and Cara
- LDoes not print Ben or Dev
- LUses the condition 50 or more, so Cara's score of exactly 50 is included
Loop over the keys and print each name whose score is at least 50. Using > 50 would wrongly leave out Cara.
scores = {"Ana": 72, "Ben": 45, "Cara": 50, "Dev": 38}
for name in scores:
if scores[name] >= 50:
print(name)Tick one box.
- LD Writing would erase the scores already saved
Write mode empties the file first; append keeps what is there.
file = open("log.txt", "w")
file.write("one\n")
file.close()
file = open("log.txt", "a")
file.write("two\n")
file.close()- L2
Write mode creates the file with one line, then append mode adds a second.
def set_score():
score = 10
set_score()
print(score)- LNameError
score only exists inside set_score(), so print(score) raises a NameError.
- LA local variable only exists inside the subroutine where it is created
- LA global variable can be used anywhere in the program
A local variable is created inside a subroutine and only exists while it runs, so nothing outside can use it. A global variable is created in the main program and can be used anywhere in it.
| List | 2D list | Dictionary | |
|---|---|---|---|
| Store each player's name with their score, to look up a score by name | |||
| Store the squares of a noughts and crosses board in rows and columns | |||
| Store a shopping list in order |
Tick one box in each row.
- LStore each player's name with their score, to look up a score by name: Dictionary
- LStore the squares of a noughts and crosses board in rows and columns: 2D list
- LStore a shopping list in order: List
A dictionary looks values up by key, a 2D list holds a grid, and a list holds items in order.
Tick one box.
- LA open("data.txt", "r")
"r" is read mode.
- LCheck the file exists first, or catch the error with try and except
- LShow a helpful message, or create an empty file, instead of crashing
Put the open() inside try and except FileNotFoundError. If the file is missing, show the user a clear message or create an empty file, so the program carries on instead of crashing.
total = 0
for row in [[1, 2], [3, 4], [5, 6]]:
total = total + row[1]
print(total)- L12
row[1] is the second item of each row: 2 + 4 + 6 = 12.
Tick one box.
- LB None
Python returns None when there is no return.
word = "Python"
print(word[0] + word[-1])- LPn
The first letter is P and the last is n.
This is an original Key Stage 3 assessment. Most questions are closed, so the marking is exact, and the written answers are marked against a mark scheme in the same style as a GCSE paper, so the format is familiar long before it counts.