Python by Projects › SAVE POINT

Exercises: SAVE POINT

A game that forgets everything when you close it is no fun. In these missions, you will teach the program to WRITE progress to a file and LOAD it back next time. Keep integer scores in savegame.txt and text settings in settings.txt.

Exercise 1: Save Space-Race Scores — `savegame.txt`

A space-race game needs a save point. Start with space_race_scores = [1375, 910, 1260, 1480, 1045]. Open savegame.txt with a with block in "w" mode. Loop over space_race_scores and write each score as text followed by "\n", so the file gets one integer score per line. Print Saved space-race scores. when the save is complete.

Real program

After the fixed version works, read the score count and each score with input(). Keep the file write and output unchanged.

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

Exercise 2: Load the File — `savegame.txt`

Open savegame.txt for reading with a with block. Use f.read() to collect the raw text, then print it. Your notebook already created this file in Exercise 1. Expected lines: 1375, 910, 1260, 1480, and 1045.

Real program

This already reads real input — it loads a save file; no separate input() version.

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

Exercise 3: Report the Loaded High Score — `savegame.txt`

Open savegame.txt with a with block and rebuild a list named high_scores using a loop, .strip(), int(), and .append(). Create score_report = {"name": "Ada", "score": max(high_scores)} using your loaded list. You must author an f-string that uses both dictionary values to print a loaded-data report in this form: Ada — high score 1480.

Real program

This already reads real input — it loads a save file; no separate input() version.

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

Exercise 4: Save Settings — `settings.txt`

Start with settings = {"player": "Ada", "volume": 8, "difficulty": "easy"}. Open settings.txt with a with block in "w" mode. Use dictionary access to write the player, volume, and difficulty as three text lines. Use str() for the volume and end every line with "\n". Print Saved Ada's settings. after the with block.

Real program

After the fixed version works, read the player, volume, and difficulty with input(). Keep the file write and output unchanged.

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

Exercise 5: Search the Settings — `settings.txt`

Open settings.txt for reading with a with block and place all its text in a variable named info. Create names_to_test = [settings["player"], "Mina"] and loop over both names. For each name, write a full if name in info / else branch. Use f-strings to welcome a found player and report that a missing player has no save. Both branches must run: Ada is present and Mina is not. Expected lines: Welcome back, Ada! and No save found for Mina. Keep this file as text; do not turn its lines into integers.

Real program

This already reads real input — it loads a save file; no separate input() version.

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

Exercise 6: Save Then Load — `savegame.txt`

Write save_scores(scores, filename). It must use a with block, "w" mode, and a loop to write one integer score per line, then return filename. Write load_scores(filename). It must use a with block, a loop, .strip(), int(), and .append() to rebuild and return a list. Call both helpers with [300, 450, 725, 1200, 900] and savegame.txt, then print the loaded result. Expected output: [300, 450, 725, 1200, 900].

Real program

After the fixed version works, read the score count and each score with input(). Keep the save-then-load round trip and output unchanged.

How this is checked: Checked by comparing what your program prints with the expected output.

Answer format: one line

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

Exercise 7: Save My Score — `savegame.txt`

Use the fixed text value sample_score below and convert it with int(). Load the integer scores from savegame.txt with your load_scores helper from Exercise 6. Write a full if new_score in saved_scores / else branch. If it is already present, print an f-string saying so. Otherwise, append it, re-save the whole list with save_scores, and print an f-string containing the new score and len(saved_scores). Expected output for the fixed value: Saved 1350; 6 scores total.

Real program

After the fixed version works, read the new score with input(). Keep the load, append, save, and output unchanged.

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

Exercise 8: Diagnose a Missing Save File

Study this traceback. Do not run the failing code.

Traceback (most recent call last):
  File "<notebook>", line 1, in <module>
    with open("dragon_slot.txt") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'dragon_slot.txt'

Edit this Markdown cell to name the missing file and explain why the error happened.

  1. The missing filename is: ____
  2. The FileNotFoundError happened because: ____

Then author the ordering fix as actual code in the code cell below. First open dragon_slot.txt in "w" mode and write "checkpoint ready\n". After that save is complete, open the file for reading, store its text in dragon_save, and print it.

More Practice (homework): Exercises 9–11

These core exercises add more save-and-load repetitions after the in-class path.

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

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

Exercise 9: Prove That `"w"` Replaces — `overwrite_scores_tmp.txt`

Start with practice_scores = [42, 77, 15, 103, 64]. Save the list to overwrite_scores_tmp.txt using a with block, "w" mode, a loop, and one score per line. Open the file for reading and store all its text in first_save. Save the same list to the same filename a second time, again using "w", then read it into second_save. Print first_save, second_save, len(first_save), and len(second_save). Do not use an assertion. Expected: both text blocks and both lengths match, with no duplicated second copy. This shows that "w" replaces the file; "a" would add to its end.

Real program

After the fixed version works, read the score count and each score with input(). Keep the two "w" saves, reads, and outputs unchanged.

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

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

Exercise 10: Save and Load a Player Profile — `settings.txt`

Start with profile = {"player": "Mina", "level": "forest"}. Write save_profile(profile, filename). Use a with block and "w" mode to write both dictionary values on separate lines, then return filename. Write load_profile(filename). Use a with block, a loop, .strip(), and .append() to build and return a list of the saved lines. Call both functions with settings.txt. Write a full if profile["level"] in loaded_profile / else branch and use f-strings to print either Mina can resume forest. or a missing-level message. Expected output: Mina can resume forest.

Real program

After the fixed version works, read the player and level with input(). Keep the save, load, branch, and output unchanged.

How this is checked: Checked by comparing what your program prints with the expected output.

Answer format: one line

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

Exercise 11: Load a Labeled Scoreboard — `savegame.txt`

Write load_scoreboard(filename, game_name). Inside the function, make an empty list, open the filename with a with block, and loop over the file. Use .strip(), int(), and .append() to rebuild the integer scores. Before returning the list, use an f-string with game_name and len(scores) to print a report. Call the function with savegame.txt and "Space Race", then print the returned list. Expected report: Space Race loaded 6 scores.

Real program

This already reads real input — it loads a save file; no separate input() version.

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

Algorithm Extension

Extra loop practice — optional enrichment: these drills reuse the same loop shapes on small fixed data, so try as many as you like.

Exercise 12: Load Scores into a List — `savegame.txt`

Make an empty list named loaded. Open savegame.txt, then use for line in f: to loop over the file line-by-line. For each line, use .strip(), int(), and .append() to rebuild a list of integer scores. Print the rebuilt list. Expected output: [300, 450, 725, 1200, 900, 1350]. Remember: this loop walks through a file, not a list.

Pattern Spotlight retrieval: Which pattern is being reused? Variation axis — file lines to integers: each file line produces one integer.

Real program

This already reads real input — it loads a save file; no separate input() version.

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

Exercise 13: Sum the saved scores — `savegame.txt`

Open savegame.txt for reading with a with block. Start a named total = 0. Loop over the file lines. On each pass, strip the line, convert it with int(), store that integer in score, and update the running total with total = total + score. Print the total after the loop. Expected saved total: 4925. Do not build a list.

Pattern Spotlight retrieval: Which pattern is this, and which variable is the total-so-far? Variation axis — data: file lines instead of in-memory numbers; the loop stays inline.

Real program

This already reads real input — it loads a save file; no separate input() version.

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

Exercise 14: Find a Setting and Stop Early — `settings.txt`

Open settings.txt for reading. Start with target_name = "Mina" and found_name = "not found". Scan the file lines one by one. Strip each line, compare it with target_name, and when they match, store the stripped name in found_name and use break to stop checking the rest. Print found_name after the loop. Expected result: Mina.

Pattern Spotlight retrieval: Which pattern checks each line and stops after a match? Variation axis — file lines and not-found case: search file lines instead of list items; if the target is missing, the stand-in stays unchanged.

Real program

After the fixed version works, read the setting name to find with input(). Keep the file scan, early stop, and output unchanged.

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

Exercise 15: Highest score by scanning — `savegame.txt`

Open savegame.txt for reading with a with block. Start best_score = 0, then scan the file lines one by one. On each pass, strip the line, convert it with int(), store that integer in score, and compare it with best_score. When score is greater, update best_score. Print best_score after the loop. Expected result: 1350. Do not build a list and do not use max(); use the explicit best-so-far scan.

Pattern Spotlight retrieval: Which pattern keeps the best value seen so far? Variation axis — input source: file lines instead of the earlier parallel lists or dictionary entries; here only the number needs to be kept.

Real program

This already reads real input — it loads a save file; no separate input() version.

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

Exercise 16: Load only the high scores — `savegame.txt`

Start with threshold = 700 and a NEW empty list named high_scores. Open savegame.txt for reading with a with block and loop over its lines. On each pass, strip the line, convert it with int(), and store the result in score. Use a condition to check whether score is at least the threshold, and append only passing scores to high_scores. Print the new list after the loop. Expected result: [725, 1200, 900, 1350]. Do not use a list comprehension.

Pattern Spotlight retrieval: Which pattern builds a new list containing only items that pass a test? Variation axis — file lines: convert saved text lines to scores before filtering them by a threshold.

Real program

This already reads real input — it loads a save file; no separate input() version.

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

Exercise 17: Count the Boss-Level Saves

The list scores = [300, 1200, 900, 1500, 700] represents save data that is already loaded. Count how many scores are >= 1000, then report the count.

Worked example: 1200 and 1500 meet the requirement, so the answer is 2.

Real program

After the fixed version works, read the score count and each score with input(). Keep the count and output unchanged.

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

Exercise 18: Average of the Saves

The list scores = [300, 450, 725, 1350] represents save data that is already loaded. Add the scores with a loop, divide the total by how many scores there are, and report the average. A float result is fine.

Worked example: The total is 300 + 450 + 725 + 1350 = 2825, and 2825 / 4 = 706.25.

Real program

After the fixed version works, read the score count and each score with input(). Keep the average calculation and output unchanged.

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

Exercise 19: Lowest Saved Score

The list scores = [900, 450, 1200, 725] represents save data that is already loaded. Scan the list while keeping track of the smallest value seen. Seed your lowest variable from the FIRST score in the list, never from 0, then report the lowest score.

Worked example: Start with 900; 450 is lower, while 1200 and 725 are not lower than 450, so the answer is 450.

Real program

After the fixed version works, read the score count and each score with input(). Keep the lowest-score scan and output unchanged.

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

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

Exercise 20: Which Save Holds My Score?

The list scores = [300, 450, 725, 1350] represents save data that is already loaded, and my_score = 725. Walk through the list with a position counter and stop at the first score equal to my_score. Report its 0-based position.

Worked example: 300 is at position 0, 450 is at position 1, and 725 is the first match at position 2, so the answer is position 2.

Real program

After the fixed version works, read the score count, each score, and my_score with input(). Keep the position search and output unchanged.

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

Exercise 21: Saves That Fit the Budget

The list scores = [300, 450, 725, 1350] represents save data that is already loaded, and budget = 1000. Follow the check-before-adding rule: add a score to the running total only if doing so would keep the total <= 1000; otherwise, stop. Report how many scores fit and the final total.

Worked example: 300 fits for a total of 300, then 450 fits for a total of 750. Adding 725 would make 1475, which is above 1000, so 2 scores fit and the total is 750.

Real program

After the fixed version works, read the score count, each score, and the budget with input(). Keep the check-before-adding rule and output unchanged.

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

Exercise 22: Saves Until the Budget Tips

Use the SAME list as Exercise 21, scores = [300, 450, 725, 1350], with budget = 1000, but apply the opposite boundary rule. Exercise 21 checks before adding; here, add each score to the running total first, then stop as soon as the total goes above 1000. Report how many scores were added, the tipping total, and the tipping score that pushed the total over the budget.

Worked example: Adding 300 gives 300, adding 450 gives 750, and adding 725 gives 1475, which tips the total above 1000. Therefore, 3 scores were added, the tipping total is 1475, and the tipping score is 725.

Real program

After the fixed version works, read the score count, each score, and the budget with input(). Keep the add-before-checking rule and output unchanged.

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

Exercise 23: First Save at or Above the Target

The list scores = [300, 450, 725, 1350] represents save data that is already loaded, and target = 700. Scan the scores in order, stop at the first score >= target, and report that score.

Worked example: 300 and 450 are below 700; 725 is the first score at or above the target, so the answer is 725.

Real program

After the fixed version works, read the score count, each score, and the target with input(). Keep the first-match search and output unchanged.

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

Exercise 24: Highest Saved Score — `savegame.txt`

  • Challenge

Load all the integer scores from savegame.txt into a list. Use max() to find and print the highest saved score. Expected highest score: 1350.

Real program

This already reads real input — it loads a save file; no separate input() version.

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

Exercise 25: Add a New High — `savegame.txt`

  • Challenge

Load the integer score list from savegame.txt, use .append() to add the fixed score 1600, and re-save the whole list. Save with "w" mode, which REPLACES the file's old contents with your updated list (a fresh full save, not added onto the old file). Print the updated list. Expected output: [300, 450, 725, 1200, 900, 1350, 1600].

Real program

After the fixed version works, read the new high score with input(). Keep the load, append, save, and output unchanged.

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 SAVE POINT