Turn Mistakes into a Practice Round

PythonBeginner
Practice Now

Introduction

Save stable answers and turn only this round's mistakes into independent practice. This unit independently prepares the three completed random/input rules from the earlier guided lab. You need those quiz concepts plus Python functions, lists, dictionaries, conditions and loops; no files or processes from an earlier VM are used.

Open the Terminal tab. Edit only the two unfinished functions in logic.py: saving and mistake selection. Preserve its three ready rules, engine.py, quiz.py and bank.json. Estimated reading and practice time: 35 minutes, not measured beginner timing.

Save Submitted Answers as JSON

In this step, play the ready quiz, then add record saving without changing its existing rules. Start in the Terminal tab:

cd /home/labex/project

The independent workspace already has working selection, option movement and input handling. Confirm normal gameplay without requesting a record yet:

python3 quiz.py bank.json --ordered --count 2

Enter 1 for sky, then 2 for tide. Both are correct and the score is 2/2. Saving and practice are the unfinished features, so this first round does not use --output or --practice.

Read the actual rules and supplied bank:

cat logic.py
cat bank.json

The ready three functions must remain intact. save_answers and wrong_questions currently raise NotImplementedError. The prepared engine returns only submitted question_id/option_id pairs. It owns feedback and score; records contain no self-reported correctness or screen number. Read the short command interface:

cat quiz.py

Follow its --output branch: after play_round, it passes the returned answers to save_answers. The shell refuses an existing output path before play. --practice calls the other unfinished function later; it is not needed to save a first round.

json.dump writes Python lists/dictionaries as JSON. The existing import json makes it available. with open(..., "w", encoding="utf-8") opens a text file for writing and closes it afterward. ensure_ascii=False keeps Unicode readable and indent=2 makes the record legible. stream.write("\n") ends it with a newline. The wrapper {"answers": answers} identifies the list.

Open the existing file:

nano logic.py

Use Ctrl+W, type def save_answers, then Enter. Keep that signature. Move down to its single raise NotImplementedError body line, press Home, then Ctrl+K once. Replace only that line with the following body; keep the four/eight-space indentation and press Enter after each line:

    with open(path, "w", encoding="utf-8") as stream:
        json.dump({"answers": answers}, stream, ensure_ascii=False, indent=2)
        stream.write("\n")

Save with Ctrl+O, Enter, wait for the written-line confirmation, then Ctrl+X to return to the shell. Leave wrong_questions unfinished until the next step. Do not replace the module or duplicate a definition.

--ordered keeps known question/choice order; --output names a new record. Play two questions: enter 1 for sky (correct), then 1 for tide (wrong):

python3 quiz.py bank.json --ordered --count 2 --output round.json
Score: 1/2 answered
Saved round.json

Display the actual record:

cat round.json

It contains sky/sun and tide/sun. The same option ID has a different meaning in the two questions. Keep this original round. If you rerun accidentally, the existing-output error is expected before any question; choose a new name for extra experiments rather than deleting the earlier record. For a syntax error, read the filename/line, reopen nano, search the affected function and correct only that local body. Save and retry. If a source path is misspelled, use ls, return to /home/labex/project and correct it; a Quiz error: ... is not a saved round.

Practice Only This Round's Mistakes

In this step, recompute mistakes from each question's own correct ID and preserve the original answers. Continue in the Terminal tab. Read the saved round.json if needed: sky/sun is correct, tide/sun is wrong, and the unanswered roots question is absent. Only tide belongs in this practice round.

A dictionary maps answered question IDs to selected option IDs. Include a question only if it was answered and its answer differs from its own correct_option_id. Correct and unanswered questions both stay out. In {answer["question_id"]: answer["option_id"] for answer in answers}, the for visits each answer and the key/value part builds a dictionary entry. This is a dictionary comprehension. A list comprehension visits questions and keeps whole objects whose if conditions are true. in selected asks whether that question ID has a submitted answer. and requires both membership and wrongness; a missing answer is not a mistake.

Open nano logic.py. Search for def wrong_questions with Ctrl+W and Enter. Preserve its signature, import json, your saved-answer function and all three ready rules. Cut only the single unfinished raise line, then enter this body with four/eight-space indentation:

    selected = {answer["question_id"]: answer["option_id"] for answer in answers}
    return [question for question in questions
            if question["question_id"] in selected
            and selected[question["question_id"]] != question["correct_option_id"]]

Press Enter after each line. Save with Ctrl+O, Enter, wait for the write confirmation, then Ctrl+X. --practice reads the original record; --output writes a separate new record. It retries all and only that round's answered mistakes, not an accumulating history:

python3 quiz.py bank.json --practice round.json --output retry.json

Only tide appears. Read the current number beside Moon, which may move, and select it. The score is 1/1. Inspect the independent records:

cat round.json retry.json

The original still has tide/sun; retry has tide/moon. The bank stays unchanged. Next create a fully correct round. Counts are positive and capped at the bank size, so 100 asks all three questions once:

python3 quiz.py bank.json --ordered --count 100 --output perfect.json

Enter 1, 2, 2 for sky, tide and roots. Practice that record:

python3 quiz.py bank.json --practice perfect.json --output empty.json
No mistakes to practice.
Score: 0/0 answered
Saved empty.json

An empty mistake set is valid. Finally start a new round and enter q before answering:

python3 quiz.py bank.json --output stopped.json

The score is 0/0 and stopped.json has an empty answers list. It must not record the question you left as a wrong answer. Preserve all five records. For an editor error, correct only the affected function while retaining the others. Use new record names for extra play, never edit saved answers to conceal a mistake.

Summary

You saved real submitted ID pairs, kept the original round and built independent mistake practice, including valid empty and stopped rounds.