Project Explanation
Project Explanation
---
- **`choices`**: This list holds multiple-choice answers for each question (labeled
as `a`, `b`, `c`, and `d`).
- **`selected_questions`**: This line picks **10 random** questions from the list
of all available questions, ensuring variety in each quiz session.
- **`score`**: This variable keeps track of the score. It starts at `0` and
increases by **10 points** for each correct answer.
- **Quiz loop**:
- For each question and corresponding answer options, the program:
- Displays the question and answer choices.
- Records the time it took the user to respond (using `time.time()`).
- If the response time is over 15 seconds, the program tells the user they were
too slow.
- If the answer is correct, it adds 10 points to the score and prints a success
message.
- If the answer is incorrect, it shows the correct answer.
- **`return score`**: At the end of the function, it **returns the total score**
the user achieved in that quiz round.
---
- **Error Handling**:
- If the file doesn’t exist (for example, if this is the first time running the
program), it handles the **`FileNotFoundError`** exception.
- If the file exists but it doesn’t contain a valid integer (e.g., if it was
corrupted or empty), the **`ValueError`** is caught and the function defaults to a
high score of `0`.
- **Return Value**:
- The function returns the **current high score** so that we can compare it with
the user’s score later.
---
- **Score Comparison**:
- If the score from this quiz is **higher** than the stored high score, it
updates the file with the new high score.
- It uses the **`write()`** method to **overwrite** the content of the file with
the new score.
- **Output Message**:
- If the user set a new high score, the program congratulates them with a
**message** (e.g., `🏆 New High Score: 90!`).
- If the score did not beat the high score, it just displays the **current
highest score**.
---
- **Quiz Replay**:
- After the quiz ends, it asks the user if they want to retry. If the answer is
`yes`, it **restarts** the quiz by calling `run_quiz()` again.
- If the answer is `no`, the program exits, thanking the user for playing.
- **Exit Condition**:
- If the user types "no" when asked about retrying, the program will **exit the
loop** and print a goodbye message.
---
- **Write to File**:
- The score is written to the file using `file.write()`. This updates the high
score in the file so that it can persist even after the program ends.
---
### **6. Time Tracking (`time.time()`)**:
This is used to track how long the user takes to answer each question, adding an
element of **time pressure**.
- **End Time**:
- After the user provides their answer, another timestamp is recorded.
- **Elapsed Time**:
- The time difference (`end_time - start_time`) gives the duration it took the
user to answer.
- **Time Limit**:
- If the time taken exceeds 15 seconds, the program automatically marks the
answer as **too slow** and does not award points.
---
- **`random.sample()`**:
- It randomly selects 10 questions from the `question_answer` list.
- `random.sample()` returns a **random subset** of the list, so each time the
quiz is played, the user sees different questions (from the pool of all available
questions).
---
- **Password input**:
- The `input()` function takes the user’s password and checks if it matches the
expected value (`"Shemmy"`).
- **Retry input**:
- After the quiz finishes, it asks if the user wants to **retry**. The program
then waits for a `yes` or `no` response and handles the flow accordingly.
---
- **`FileNotFoundError`**: When the file doesn’t exist, it defaults the high score
to `0` instead of crashing the program.
---
### In Conclusion:
- **The quiz** involves displaying random questions, getting user input, checking
answers, and scoring based on correct responses.
- **High score tracking** involves saving the best score to a text file, comparing
each quiz result to that score, and updating the file when needed.
- **Replayability** is handled through a loop, so the user can take the quiz again
without restarting the program.
- **Time tracking** adds an element of urgency, while **error handling** ensures
that the program doesn't break unexpectedly.
---
### Now you're **fully equipped** to explain every function and concept to your
teacher! You can say with confidence that this code is designed to be both
functional and user-friendly, while **storing important data** (like the high
score) between quiz sessions. 🏆💯