forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz.py
More file actions
62 lines (44 loc) · 1.71 KB
/
Copy pathquiz.py
File metadata and controls
62 lines (44 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# quiz.py
import pathlib
import random
from string import ascii_lowercase
try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
NUM_QUESTIONS_PER_QUIZ = 5
QUESTIONS_PATH = pathlib.Path(__file__).parent / "questions.toml"
def run_quiz():
questions = prepare_questions(
QUESTIONS_PATH, num_questions=NUM_QUESTIONS_PER_QUIZ
)
num_correct = 0
for num, question in enumerate(questions, start=1):
print(f"\nQuestion {num}:")
num_correct += ask_question(question)
print(f"\nYou got {num_correct} correct out of {num} questions")
def prepare_questions(path, num_questions):
questions = tomllib.loads(path.read_text())["questions"]
num_questions = min(num_questions, len(questions))
return random.sample(questions, k=num_questions)
def ask_question(question):
correct_answer = question["answer"]
alternatives = [question["answer"]] + question["alternatives"]
ordered_alternatives = random.sample(alternatives, k=len(alternatives))
answer = get_answer(question["question"], ordered_alternatives)
if answer == correct_answer:
print("⭐ Correct! ⭐")
return 1
else:
print(f"The answer is {correct_answer!r}, not {answer!r}")
return 0
def get_answer(question, alternatives):
print(f"{question}?")
labeled_alternatives = dict(zip(ascii_lowercase, alternatives))
for label, alternative in labeled_alternatives.items():
print(f" {label}) {alternative}")
while (answer_label := input("\nChoice? ")) not in labeled_alternatives:
print(f"Please answer one of {', '.join(labeled_alternatives)}")
return labeled_alternatives[answer_label]
if __name__ == "__main__":
run_quiz()