-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
101 lines (84 loc) · 3.25 KB
/
main.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
import json
import sys
from dataclasses import dataclass
from typing import List
@dataclass
class QuizQuestion:
question: str
# Ordered list of answer variants.
variants: List[str]
# Zero based number of the right answer in variants list.
correct_answer: int
@dataclass
class Quiz:
# Name of the quiz.
name: str
# A brief description of the quiz.
description: str
# A list of quiz questions.
quiz_questions: List[QuizQuestion]
def _AskQuestion(quize_question: QuizQuestion):
"""Asks user a question.
Returns:
True if user gave a wrong answer; otherwise, returns False.
"""
print(f'Question: [{quize_question.question}]')
for no, variant in enumerate(quize_question.variants):
print(f'{no + 1}: {variant}')
correct_answer = quize_question.correct_answer + 1
answer = int(input('Your answer:'))
if answer == correct_answer:
print('The answer is correct!\n')
return False
print(f'The answer is incorrect. The correct answer is {correct_answer}.\n')
return True
def _PlayQuiz(quiz: Quiz):
print(f'Starting [{quiz.name}]!')
print(f'Description: [{quiz.description}]')
print()
# Number of rounds user needed to finish a quiz.
rounds_number = 0
# Set of question user gave incorrect answer on. So,
# these question need to be replayed.
need_to_replay = quiz.quiz_questions.copy()
while len(need_to_replay):
rounds_number += 1
questions_count = len(need_to_replay)
print('On this intereation you need to give answer '
f'on {questions_count} quesitons.')
# List of question on which user gave wrong answer.
wrong_answers = []
for question in need_to_replay:
if _AskQuestion(question):
wrong_answers.append(question)
need_to_replay = wrong_answers
print(f'Congratulations - after {rounds_number} round quiz is over!')
def _ParseKeywordFromData(data, filename: str, keyword: str):
if keyword not in data:
raise ValueError(f'[{filename}] has invalid format, [{keyword}] keyword absents.')
return data[keyword]
def _ParseQuizQuestion(data, filename: str)->List[QuizQuestion]:
json_questions = _ParseKeywordFromData(data, filename, 'quiz_questions')
parsed_questions = []
for json_question in json_questions:
parsed_questions.append(QuizQuestion(
question=_ParseKeywordFromData(json_question, filename, 'question'),
variants=_ParseKeywordFromData(json_question, filename, 'variants'),
correct_answer=int(_ParseKeywordFromData(json_question, filename, 'correct_answer')),
))
return parsed_questions
def _ReadQuiz(filename: str)->Quiz:
with open(filename) as f:
data = json.load(f)
quiz = Quiz(
name=_ParseKeywordFromData(data, filename, 'name'),
description=_ParseKeywordFromData(data, filename, 'description'),
quiz_questions=_ParseQuizQuestion(data, filename))
return quiz
if (len(sys.argv) > 2):
raise SystemExit('Extected strictly two arguments. usage: @> main.py filename.')
filename = sys.argv[1]
print('Welcome to QuizPlayer!')
print(f'Reading quiz from file [{filename}]')
quiz = _ReadQuiz(filename)
_PlayQuiz(quiz)