Python, Concept by Concept › Checkpoint 05: Files & Objects

Questions: Checkpoint 05: Files & Objects

Complete all seven questions. Define each requested function or class, return the promised values, then use every worked sample to check your work. Each file question names its own scratch file and writes known content before reading it. Do not use input().

Each question ends with a Real version note showing how the program can read its values with input().

Question 1: Score File Round Trip

Specification: Define save_scores(scores, path). Open path in "w" mode with a with statement, loop over scores, write one score per f-string line, then return path. Define load_scores(path). Open path in explicit "r" mode with a with statement, loop over its lines, clean each line with .strip(), convert it with int(), append it to a new list, and return that list.

Use only q1_scores.txt. Call save_scores before load_scores so the question is self-contained.

Worked sample calls:

save_scores([14, 27, 31], "q1_scores.txt") -> "q1_scores.txt"
load_scores("q1_scores.txt") -> [14, 27, 31]
save_scores([5, 8], "q1_scores.txt") -> "q1_scores.txt"
load_scores("q1_scores.txt") -> [5, 8]

Real program

The real program reads space-separated scores with input() on one line, then saves and reloads them from q1_scores.txt.

How this is checked: Checked by tests that run your functions save_scores, load_scores.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Question 2: Load a Score Summary

Specification: First create q2_scores.txt in "w" mode from the fixed list [12, 19, 7, 19], using one f-string line per score. Then define score_summary(path). Open the path in explicit "r" mode. Build an integer list with for line in f:, .strip(), int(), and .append(). Use a loop to accumulate the total and count scores at least 15. Use max(scores) for the highest score. Return [total, highest, strong_count], where strong_count is the count of scores greater than or equal to 15.

Worked sample call:

score_summary("q2_scores.txt") -> [57, 19, 2]

Real program

The real program reads space-separated scores with input() on one line, then writes them to q2_scores.txt.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Question 3: Find the First Matching Team

Specification: First create q3_teams.txt in "w" mode from the fixed list ["Comet", "Otter", "Falcon"], writing one f-string line per team. Then define find_team(path, target). Open the path in explicit "r" mode. Use exactly the line-by-line search shape for line in f:, followed by if line.strip() == target: and an immediate return of f"Found {target}". After the loop, return f"No team named {target}".

Worked sample calls:

find_team("q3_teams.txt", "Otter") -> "Found Otter"
find_team("q3_teams.txt", "Lynx")  -> "No team named Lynx"

Real program

The real program reads space-separated team names on one line and a target team on the next line with input().

How this is checked: Checked by tests that run your function find_team.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Question 4: Make a Point

Specification: Define Point with __init__(self, x, y). Store both parameters as attributes. Construct the two sample objects, then read the named attributes.

Worked sample calls and reads:

first = Point(6, 2)
second = Point(-3, 8)
first.x  -> 6
first.y  -> 2
second.x -> -3

Real program

The real program reads an x coordinate and a y coordinate on separate lines with input().

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Question 5: Rectangle Report

Specification: Define Rectangle with __init__(self, width, height) and store width and height as attributes. Add area(self) that returns the computed area. Add describe(self) that returns an f-string in the exact form "width by height has area area".

Worked sample calls:

poster = Rectangle(8, 3)
poster.area()     -> 24
poster.describe() -> "8 by 3 has area 24"
Rectangle(2.5, 4).area() -> 10.0

Real program

The real program reads a width and a height on separate lines with input().

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Question 6: Counter Award Band

Specification: Define Counter with __init__(self, count) and store count as an attribute. Add award_band(self) with one if/elif/else ladder: return "gold" when count is at least 10, "silver" when count is at least 5, and "bronze" otherwise. Add reached_goal(self) that returns the boolean result of comparing count with 10.

Worked sample calls:

Counter(12).award_band() -> "gold"
Counter(7).award_band()  -> "silver"
Counter(2).award_band()  -> "bronze"
Counter(12).reached_goal() -> True
Counter(7).reached_goal()  -> False

Real program

The real program reads a counter value on one line with input().

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Question 7: Save a Labeled Rectangle

Specification: Define Rectangle with code, width, and height attributes. Add short_code(self) that returns the attribute slice self.code[0:2]. Add save(self, path). The save method opens path in "w" mode with a with statement, writes the three attributes on separate f-string lines, then returns path.

Use only q7_rectangle.txt. Make the sample rectangle and call its save method before opening that path in explicit "r" mode. Read the saved text with f.read() to confirm all three lines.

Worked sample calls and reads:

sign = Rectangle("NW-17", 9, 4)
sign.short_code() -> "NW"
sign.save("q7_rectangle.txt") -> "q7_rectangle.txt"
saved_text -> "NW-17\n9\n4\n"

Real program

The real program reads a rectangle code, width, and height with input() on separate lines, then saves and reads q7_rectangle.txt.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Back to Checkpoint 05: Files & Objects