@@ -11,7 +11,7 @@ def __init__(self):
11
11
self .__chosen_subject = 0
12
12
self .__subject_questions = []
13
13
self .__subject_answers = []
14
- self .__number_of_subject_questions = 0
14
+ self .__number_of_questions_in_subject = 0
15
15
16
16
self .__random_subject = len (self .__subjects ) - 1
17
17
self .__random_questions = []
@@ -25,7 +25,6 @@ def __init__(self):
25
25
self .__quit_session = False
26
26
27
27
def start (self ):
28
- """Start the Q&A session."""
29
28
if not self .__quit_session :
30
29
self .__display_intro ()
31
30
self .__display_subjects ()
@@ -39,7 +38,6 @@ def start(self):
39
38
self .start ()
40
39
41
40
def __display_intro (self ):
42
- """Print the program title and instructions."""
43
41
if not self .__intro_displayed :
44
42
title_length = 35
45
43
print ("=" * title_length )
@@ -51,77 +49,66 @@ def __display_intro(self):
51
49
self .__intro_displayed = True
52
50
53
51
def __display_subjects (self ):
54
- """Print the available subjects for a Q&A session."""
55
52
print ("Choose your subject:" )
56
53
for i , j in enumerate (self .__subjects ):
57
54
print (f"{ (i + 1 ):>2} . { j } " )
58
55
print ("On any question, input 'q' to quit." )
59
56
60
57
def __choose_subject (self ):
61
- """Ask the user to choose the Q&A subject."""
62
58
chosen_subject = input ()
63
59
try :
64
60
self .__check_valid_subject (chosen_subject )
65
61
except ValueError :
66
62
self .__check_quit_session (chosen_subject )
67
63
68
64
def __check_valid_subject (self , chosen_subject ):
69
- """Check if the user has chosen a valid Q&A subject."""
70
65
chosen_subject = int (chosen_subject )
71
66
if 1 <= chosen_subject <= len (self .__subjects ):
72
67
self .__chosen_subject = chosen_subject - 1
73
68
else :
74
69
self .__invalid_subject ()
75
70
76
71
def __check_quit_session (self , chosen_subject ):
77
- """Check if the user has chosen to quit the Q&A session."""
78
72
if chosen_subject .lower () == 'q' :
79
73
self .__display_score ()
80
74
self .__quit_session = True
81
75
else :
82
76
self .__invalid_subject ()
83
77
84
78
def __invalid_subject (self ):
85
- """Indicate that an invalid subject has been chosen. Take user to choose another subject."""
86
79
print ("Invalid option. Please choose again." )
87
80
self .__choose_subject ()
88
81
89
82
def __build_qa_session (self ):
90
- """Build a subject or random Q&A session, based on user's choice."""
91
83
if self .__chosen_subject == self .__random_subject :
92
84
self .__build_random_qa_session ()
93
85
else :
94
86
self .__build_subject_qa_session ()
95
87
96
88
def __build_subject_qa_session (self ):
97
- """Build a subject Q&A session."""
98
89
self .__clear_subject_qa_session ()
99
90
self .__build_subject_questions ()
100
91
self .__build_subject_answers ()
101
92
102
93
def __clear_subject_qa_session (self ):
103
- """Clear lists of subject questions and answers."""
104
94
self .__subject_questions .clear ()
105
95
self .__subject_answers .clear ()
106
96
107
97
def __build_subject_questions (self ):
108
- """Retrieve questions from the chosen subject."""
109
98
filename = self .__subjects_folder \
110
99
+ self .__subjects [self .__chosen_subject ].lower ().replace (' ' , '_' ) \
111
100
+ "_questions.txt"
112
101
with open (filename ) as file_object :
113
102
self .__subject_questions = file_object .readlines ()
114
103
115
104
def __build_subject_answers (self ):
116
- """Retrieve answers from the chosen subject."""
117
105
filename = self .__subjects_folder \
118
106
+ self .__subjects [self .__chosen_subject ].lower ().replace (' ' , '_' )\
119
107
+ "_answers.txt"
120
108
with open (filename ) as file_object :
121
109
self .__subject_answers = file_object .readlines ()
122
110
123
111
def __build_random_qa_session (self ):
124
- """Retrieve random questions and answers."""
125
112
self .__clear_random_qa_session ()
126
113
while len (self .__random_questions ) < self .__number_of_random_questions :
127
114
self .__choose_random_subject ()
@@ -133,51 +120,41 @@ def __build_random_qa_session(self):
133
120
self .__reset_random_subject ()
134
121
135
122
def __clear_random_qa_session (self ):
136
- """Clear lists of random questions and answers."""
137
123
self .__random_questions .clear ()
138
124
self .__random_answers .clear ()
139
125
140
126
def __choose_random_subject (self ):
141
- """Choose a random subject within the range of available subjects."""
142
127
self .__chosen_subject = randrange (self .__random_subject )
143
128
144
129
def __choose_random_question_number (self ):
145
- """Choose a question number within the range of available questions."""
146
130
self .__random_question_number = randrange (len (self .__subject_questions ))
147
131
148
132
def __build_random_question (self ):
149
- """Add a new question to the random questions list."""
150
133
if self .__subject_questions [self .__random_question_number ] not in self .__random_questions :
151
134
self .__random_questions .append (self .__subject_questions [self .__random_question_number ])
152
135
153
136
def __build_random_answer (self ):
154
- """Add the corresponding answer to the random answers list."""
155
137
if self .__subject_answers [self .__random_question_number ] not in self .__random_answers :
156
138
self .__random_answers .append (self .__subject_answers [self .__random_question_number ])
157
139
158
140
def __copy_random_questions_and_answers (self ):
159
- """Copy random questions and answers to subject questions and answers."""
160
141
self .__subject_questions = self .__random_questions
161
142
self .__subject_answers = self .__random_answers
162
143
163
144
def __reset_random_subject (self ):
164
- """Reset the chosen subject to the random subject."""
165
145
self .__chosen_subject = self .__random_subject
166
146
167
147
def __display_subject_title (self ):
168
- """Print the chosen subject title and the number of questions."""
169
- self .__set_number_of_subject_questions ()
170
- print (f"\n --- { self .__subjects [self .__chosen_subject ]} : { self .__number_of_subject_questions } questions ---" )
148
+ self .__set_number_of_questions_in_subject ()
149
+ print (f"\n --- { self .__subjects [self .__chosen_subject ]} : { self .__number_of_questions_in_subject } questions ---" )
171
150
172
- def __set_number_of_subject_questions (self ):
173
- """Set the total number of questions that the Q&A session will go through."""
151
+ def __set_number_of_questions_in_subject (self ):
174
152
if self .__chosen_subject == self .__random_subject :
175
- self .__number_of_subject_questions = self .__number_of_random_questions
153
+ self .__number_of_questions_in_subject = self .__number_of_random_questions
176
154
else :
177
- self .__number_of_subject_questions = len (self .__subject_questions )
155
+ self .__number_of_questions_in_subject = len (self .__subject_questions )
178
156
179
157
def __ask_questions (self ):
180
- """Ask the list of questions, one by one."""
181
158
for question_number , question in enumerate (self .__subject_questions ):
182
159
print (f"Q{ question_number + 1 } . { question [:- 1 ]} " )
183
160
response = self .__check_answer (question_number )
@@ -188,7 +165,6 @@ def __ask_questions(self):
188
165
self .__compute_score (response )
189
166
190
167
def __check_answer (self , question_number ):
191
- """Check if the user inputs the correct or incorrect answer, or decides to quit."""
192
168
answer = input ()
193
169
if answer == self .__subject_answers [question_number ][:- 1 ]:
194
170
print ("Correct!" )
@@ -201,14 +177,12 @@ def __check_answer(self, question_number):
201
177
return "incorrect"
202
178
203
179
def __compute_score (self , response ):
204
- """Compute user's score based on correct and incorrect answers."""
205
180
if response == "correct" :
206
181
self .__correct_answers += 1
207
182
elif response == "incorrect" :
208
183
self .__incorrect_answers += 1
209
184
210
185
def __display_score (self ):
211
- """Display user's score if there is one."""
212
186
total_answers = self .__correct_answers + self .__incorrect_answers
213
187
if total_answers > 0 :
214
188
print ("\n --- Results ---" )
@@ -218,7 +192,6 @@ def __display_score(self):
218
192
print (f"Accuracy rate: { accuracy :.2%} " )
219
193
220
194
def __ask_to_continue (self ):
221
- """Ask the user if he wants to continue with a new Q&A session."""
222
195
if not self .__quit_session :
223
196
print ("\n Would you like to continue with another subject? (y/n)" )
224
197
continue_with_qa = input ()
0 commit comments