-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz_brain.py
113 lines (93 loc) · 4.02 KB
/
quiz_brain.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
class QuizBrain:
"""
A class representing a quiz game.
Attributes:
question_number (int): The current question number.
question_list (list): The list of questions and choices.
score (int): The player's score.
"""
def __init__(self, q_list):
"""
Initialize a new QuizBrain instance.
Args:
q_list (list): A list of dictionaries representing questions and choices.
"""
self.question_number = 0
self.question_list = q_list
self.score = 0
def still_has_questions(self):
"""
Check if there are more questions to answer.
Returns:
bool: True if there are more questions, False otherwise.
"""
return self.question_number < len(self.question_list)
def next_question(self):
"""
Display the next question and handle user input for answers.
- Shows the current question and choices.
- Validates and processes user input for answers, 'state', or 'off'.
- Updates the score and question number based on user input.
"""
while self.question_number < len(self.question_list):
current_question = self.question_list[self.question_number]
self.question_number += 1
# Show the current question
print(f'Q.{self.question_number}: {current_question["question"]}')
# Show Choices
for i, choice in enumerate(current_question["choices"], start=1):
print(f"{i}. {choice}", end='\t\t')
print()
while True:
user_ans = input(
"\nEnter the number of your answer (e.g., 1, 2, 3, ...)\n\t- 'state' to see your progress"
"\n\t- 'off' to end the quiz\nEnter your answer: ").strip()
if user_ans.isdigit():
user_ans = int(user_ans)
if 1 <= user_ans <= len(current_question["choices"]):
user_ans = current_question["choices"][user_ans - 1]
self.check_ans(user_ans, current_question["correct_answer"])
break # Break the loop and proceed to the next question
else:
print("\nInvalid choice. Please enter a valid number.")
else:
if user_ans == "state":
print(self.state()) # Show progress
# Present the same question again
self.question_number -= 1
break
elif user_ans == "off":
self.question_number = len(self.question_list)
break # End the quiz
else:
print("\nInvalid input.")
print()
def check_ans(self, user_ans, correct_ans):
"""
Check if the user's answer is correct and update the score.
Args:
user_ans (str): The user's answer.
correct_ans (str): The correct answer for the current question.
"""
if user_ans.lower().strip() == correct_ans.lower().strip():
self.score += 1
def state(self):
"""
Get the user's progress and remaining questions.
Returns:
str: A message with the user's progress and remaining questions.
"""
return (f'\n=> You have {self.get_score()} correct answers, '
f'{self.question_number - self.get_score() - 1} wrong answers, '
f'and {len(self.question_list) - self.question_number + 1} remaining questions.')
def get_score(self):
"""
Get the player's score.
Returns:
int: The player's score.
"""
return self.score
def score_details(self):
return (f"\n\t- Total questions: {len(self.question_list)}"
f"\n\t- Solved question(s): {self.score}"
f"\n\t- Wrong answers: {self.question_number - self.get_score() - 1}")