-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.py
283 lines (241 loc) · 9.42 KB
/
project.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""Simple quiz game."""
import json
import logging
import random
from argparse import ArgumentParser, Namespace
from sys import argv, exit
from typing import Annotated, Optional
from validator_collection import errors, validators
def main():
"""Main function."""
logging.basicConfig(
format='%(levelname)s: %(message)s',
level=logging.CRITICAL)
if argv[1:]:
logging.debug("Send arguments from CLI: %s", argv[1:])
args = parse_args(argv[1:])
else:
logging.warning("Send no arguments")
args = parse_args()
logging.debug("Returned form parsing args: %s", args)
match args.action:
case "play":
logging.debug("Send to play game function: %s", args.level)
play_game(args.level)
case "list":
logging.debug("Sent to list questions function")
list_questions()
case "add":
logging.debug("Sent to add question function")
add_question()
case "delete":
logging.debug(
"Send to delete question function: %s", args.question_no)
delete_question(args.question_no)
def parse_args(
args: Optional[list[str]] = ["play"]) -> Optional[Namespace]:
"""Parse the arguments and return a namespace with options."""
parser = ArgumentParser(description="A simple quiz game")
subparsers = parser.add_subparsers(dest='action', required=True)
parser_play = subparsers.add_parser(
name="play",
help="Play the game and optionally specify a level (default 1)")
parser_play.add_argument(
"-l", "--level",
help="Game level: 1(easy) - 3(hard)",
type=int,
choices=range(1, 4),
default="1")
subparsers.add_parser(
name="list",
help="List all the questions in the game")
subparsers.add_parser(
name="add",
help="Add a question to the game")
parser_delete = subparsers.add_parser(
name="delete",
help="Delete a question from the game")
parser_delete.add_argument(
"question_no",
help="Question number to detele (first list questions)",
type=int)
return parser.parse_args(args)
def play_game(level: Annotated[int, range(1, 4)]) -> None:
"""Start the game with difficulty set at `level`."""
logging.info("Start game with level: %s", level)
ROUNDS = 10
questions = open_json()
if len(questions) < ROUNDS:
exit("Not enough questions")
score = 0
def print_score() -> None:
print(f"\nFinal score: {score}")
print(f"Difficulty level: {level}/3")
if score == 10:
print("Perfect game!!!")
elif score >= 8:
print("Good game")
elif score == 7:
print("Pretty good game")
elif score >= 5:
print("Maybe you can do better")
elif score >= 3:
print("Have another try")
elif score >= 1:
print("You really should have another try")
else:
print("Are you even trying?")
for round_no in range(1, ROUNDS + 1):
# print the question
print(f"\nQuestion {round_no}/{ROUNDS}")
question = questions.pop(random.randint(0, len(questions) - 1))
print(question["name"])
logging.debug(question["name"])
# print the variants
variants: list = (question["answ_bad"][level-1:level+2] +
[question["answ_good"]])
random.shuffle(variants)
for poz, variant in enumerate(variants):
print(f"({poz + 1}) {variant}")
# logging correct answer
logging.debug(variants.index(question["answ_good"]) + 1)
# get answer from user
while True:
answer = input("Your answer (1, 2, 3 or 4): ").lower()
if answer in {"1", "2", "3", "4", "quit"}:
if answer == "quit":
print_score()
exit("Game ended")
break
else:
print("Enter '1', '2', '3' or '4'.\nEnter 'quit' to quit game")
# check answer
if variants[int(answer) - 1] == question["answ_good"]:
score += 1
print("✅ Good job!")
else:
print(f"❗️ Sorry, the correct answer was {question['answ_good']}")
logging.info("Score: %s", score)
print_score()
def list_questions() -> None:
"""List all questions indexed."""
logging.info("List all questions")
questions = open_json()
for poz, question in enumerate(questions):
print(f"{poz + 1:>4} - {question['name']}")
logging.debug("'%s' %s", question['answ_good'], question['answ_bad'])
def add_question() -> None:
"""Add a question to the file."""
logging.info("Add a question")
print("You need to provide:",
" - a question",
" - 1 correct answer",
" - 3 incorrect easy answers",
" - 1 incorrect medium answer",
" - 1 incorrect hard answer", sep="\n")
question = {"name": "", "answ_good": "", "answ_bad": []}
input_questions = {
"Question: ": "name",
"Correct answer: ": "answ_good",
"First incorrect easy answer: ": "answ_bad",
"Second incorrect easy answer: ": "answ_bad",
"Third incorrect easy answer: ": "answ_bad",
"Incorrect medium answer: ": "answ_bad",
"Incorrect hard answer: ": "answ_bad"
}
for in_q in input_questions:
while True:
try:
answer = validators.string(input(in_q).strip())
if input_questions[in_q] == "answ_bad":
if (answer == question["answ_good"] or
answer in question["answ_bad"]):
print("Allready added this answer!")
continue
question[input_questions[in_q]].append(answer)
else:
question[input_questions[in_q]] = answer
break
except errors.EmptyValueError:
logging.debug("Empty value for: '%s'", input_questions[in_q])
logging.debug("New question to add: %s", question)
questions = open_json()
questions.append(question)
if save_json(questions):
print("Question added")
logging.info("Added question: %s", question)
else:
print("Question not added... Try again")
def delete_question(question_no: int) -> None:
"""Delete `question_no` from the file."""
logging.info("Delete question: %s", question_no)
questions = open_json()
# validate question number and retrieve the index question
try:
question_index = validators.integer(
question_no - 1,
minimum=0,
maximum=len(questions) - 1)
except errors.MinimumValueError:
logging.debug("%s < minimum allowed (0)", question_no)
exit("Minimum question number is 1")
except errors.MaximumValueError:
logging.debug("%s > maximum allowed (%s)", question_no, len(questions))
exit(f"Maximum question number is {len(questions)}")
# confirmation
confirm = input(f"This will delete question number {question_no}:\n " +
questions[question_index]["name"].rstrip("?") +
"\nAre you sure? (y)es: ")
if confirm in {"yes", "y"}:
del questions[question_index]
if save_json(questions):
logging.info("Deleted question: %s", question_no)
print("Question was deleted")
else:
print("Question was not deleted")
else:
print("Question was not deleted")
def open_json(filename: str = "questions.json") -> list[dict[str, str | list]]:
"""Open the question json file."""
logging.info("Opening json: %s", filename)
try:
with open(filename, encoding="UTF-8") as json_file:
questions = json.load(json_file)
with open("json_schema.json", encoding="UTF-8") as schema_file:
schema = json.load(schema_file)
validators.json(questions, schema)
return questions
except FileNotFoundError as err:
logging.debug(err)
logging.critical("File '%s' not found", filename)
except json.JSONDecodeError as err:
logging.debug(err)
logging.critical("File '%s' is not a correct json file", filename)
except errors.JSONValidationError as err:
logging.debug(err)
logging.critical("There is invalid data in '%s'", filename)
exit("Quit because of fatal error")
def save_json(
questions: list[dict[str, str | list]],
filename: str = "questions.json") -> Optional[bool]:
"""Save questions to the json file."""
logging.info("Saving json: %s", filename)
try:
with open("json_schema.json", encoding="UTF-8") as schema_file:
schema = json.load(schema_file)
validators.json(questions, schema)
with open(filename, "w+", encoding="UTF-8") as json_file:
json.dump(questions, json_file, indent=2)
return True
except FileNotFoundError as err:
logging.debug(err)
logging.warning("File '%s' not found", filename)
except json.JSONDecodeError as err:
logging.debug(err)
logging.warning("File '%s' is not a correct json file", json_file)
except errors.JSONValidationError as err:
logging.debug(err)
logging.critical("Element was not validated")
exit("Quit because of fatal error")
if __name__ == "__main__":
main()