homework.wenqian.dev
← Back to index
Exam 22026-08-13

Dict and File Practical Exam

60 minutes · 60 points · dict lookup, nested dict, counting, file modes, debugging, and a save/load pair

Instructions

Scope: This exam covers Tutorial 4 only: dict, dict inside dict, and reading and writing text files. You may use for, if, in, not in, del, len, int, str, split, open, with, and try / except. Write the loops yourself: .get(), .items(), .values() and .keys() are not allowed. No class is needed anywhere.

Time

60 min

Total

60 pts

Language

Python

No shortcuts

no .get()/.items()

Teacher note: Sections A to D reward reading code carefully. A student who understood the tutorial but writes slowly can still pass on those alone.

Section A — Code Tracing

3 questions, 4 points each. Write the exact output and one short reason.

Q1

Add, change, delete

4 pts

Two of these lines look identical but do different jobs. Say which is which.

python
bag = {"rope": 2, "lamp": 1}bag["lamp"] = 5bag["torch"] = 1del bag["rope"]print(len(bag))print(bag)print("rope" in bag)
Q2

A dict inside a dict

4 pts
python
rooms = {    "hall": {"name": "Great Hall", "exits": {"north": "library"}},    "library": {"name": "Library", "exits": {"south": "hall"}},}print(rooms["library"]["name"])print(rooms["hall"]["exits"]["north"])rooms["hall"]["exits"]["east"] = "kitchen"print(rooms["hall"]["exits"])
Q3

Counting, then looping

4 pts

Six printed lines in total. The last loop is the one students most often get wrong.

python
items = ["bolt", "rope", "bolt", "bolt", "rope"]counts = {}for item in items:    if item not in counts:        counts[item] = 0    counts[item] = counts[item] + 1print(counts)print(counts["bolt"])for key in counts:    print(key)

Section B — Fill And Complete

4 questions, 14 points total. Fill every blank so the comment on the right comes true.

Q4

Look up a room safely

3 pts

A room id that is not a key must not crash the program.

python
ROOMS = {    "hall": {"name": "Great Hall"},    "library": {"name": "Library"},}def room_name(room_id):    if room_id ____ ROOMS:        return "Unknown Room"    return ROOMS[room_id]____print(room_name("library"))   # Libraryprint(room_name("attic"))     # Unknown Room
Q5

Two lookups deep

3 pts

Return the room you arrive at, or None when that direction has no exit.

python
ROOMS = {    "hall": {"name": "Great Hall", "exits": {"north": "library"}},    "library": {"name": "Library", "exits": {"south": "hall"}},}def next_room(room_id, direction):    exits = ROOMS[room_id]["exits"]    if direction not in ____:        return ____    return exits[____]print(next_room("hall", "north"))   # libraryprint(next_room("hall", "east"))    # None
Q6

A bag that counts

4 pts

A brand new item must end up at 1, and a repeat must count up.

python
def add_item(bag, item):    if item not in bag:        bag[item] = ____    bag[item] = ____ + 1bag = {}add_item(bag, "rope")add_item(bag, "rope")add_item(bag, "lamp")print(bag)     # {'rope': 2, 'lamp': 1}
Q7

Use one up and tidy the bag

4 pts

Once a count reaches 0, the key must disappear completely.

python
def remove_item(bag, item):    if item not in bag:        return ____    bag[item] = bag[item] - 1    if bag[item] == 0:        ____ bag[item]    return Truebag = {"rope": 2, "lamp": 1}print(remove_item(bag, "lamp"))    # Trueprint(bag)                         # {'rope': 2}print(remove_item(bag, "torch"))   # False

Section C — Choices

5 questions, 12 points total. Q8-Q10 are single choice. Q11-Q12 are multiple choice.

Q8

Single choice: which line adds a pair?

2 pts

Starting from bag = {"rope": 2}, which line ADDS a new pair rather than touching an existing one?

  • A. bag["rope"] = 5
  • B. bag["lamp"] = 1
  • C. del bag["rope"]
  • D. print(bag["rope"])
Q9

Single choice: what does the loop give you?

2 pts
python
bag = {"rope": 2, "lamp": 1}for x in bag:    print(x)
  • A. The values 2 and 1
  • B. The keys "rope" and "lamp"
  • C. Pairs such as ("rope", 2)
  • D. Nothing, a dict cannot be looped
Q10

Single choice: exact output

2 pts
python
stock = {"a": 1, "b": 2}stock["a"] = stock["a"] + stock["b"]print(stock)
  • A. {'a': 1, 'b': 2}
  • B. {'a': 3, 'b': 2}
  • C. {'a': 2, 'b': 2}
  • D. 3
Q11

Multiple choice: which lines crash?

3 pts

Each line runs on its own, starting from bag = {"rope": 2}. Which ones raise KeyError?

  • A. print(bag["rope"])
  • B. print(bag["lamp"])
  • C. bag["lamp"] = 1
  • D. del bag["lamp"]
Q12

Multiple choice: files

3 pts

Which statements are TRUE?

  • A. open(path, "w") empties the file even before you write anything.
  • B. open(path, "a") keeps the old lines and writes at the end.
  • C. open(path, "r") creates the file when it is missing.
  • D. write() puts a line break at the end of every call.

Section D — Debugging

3 questions, 4 points each. For each bug: say what it prints now, name the bad line, and write the fix.

Q13

The log keeps losing its history

4 pts
python
def append_log(path, message):    with open(path, "w") as log_file:        log_file.write(message + "\n")append_log("log.txt", "opened the door")append_log("log.txt", "took the keycard")with open("log.txt", "r") as log_file:    print(log_file.read())
Q14

Adding up numbers from a file

4 pts
python
with open("scores.txt", "w") as score_file:    score_file.write("ana 30\n")    score_file.write("bo 12\n")total = 0with open("scores.txt", "r") as score_file:    for line in score_file:        parts = line.split()        total = total + parts[1]print(total)
Q15

Searching for a value

4 pts

This should print True, because one room really is red.

python
def has_color(rooms, color):    if color in rooms:        return True    return Falserooms = {"hall": "red", "library": "blue"}print(has_color(rooms, "red"))

Section E — Short Answer And Coding

2 questions, 10 points total. Use clear steps and simple Python.

Q16

Describe the round trip

4 pts

In 4-6 sentences, describe both directions. Name the Python pieces you would use, and say where str() and int() are needed.

python
bag = {"potion": 2, "keycard": 1}# The save file has to end up looking exactly like this:##   item potion 2#   item keycard 1## (a) How do you turn the bag into those lines?# (b) How do you turn those lines back into a bag?
Q17

Write save_scores and load_scores

6 pts

Complete both functions. Saving then loading must give back exactly the same dict, and a missing file must not crash.

python
def save_scores(path, scores):    # Input:    #   path   - a file name, for example "scores.txt"    #   scores - a dict, for example {"ana": 30, "bo": 12}    # Output:    #   Return nothing. Write one line per pair, like:  ana 30    #   Running it a second time must REPLACE the old file, not grow it.    passdef load_scores(path):    # Input:    #   path - a file name    # Output:    #   Return a dict rebuilt from the file.    #   Return an empty dict {} when the file does not exist.    passsave_scores("scores.txt", {"ana": 30, "bo": 12})print(load_scores("scores.txt"))# expected: {'ana': 30, 'bo': 12}print(load_scores("missing.txt"))# expected: {}