@@ -74,26 +74,34 @@ and then testing and fixing. Here are my versions of them:
7474def ask_questions (answers ):
7575 correct = []
7676 wrong = []
77+
7778 for question, answer in answers.items():
7879 if input (question + ' = ' ).strip() == answer:
7980 print (" Correct!" )
8081 correct.append(question)
8182 else :
8283 print (" Wrong! The correct answer is %s ." % answer)
8384 wrong.append(question)
85+
8486 return (correct, wrong)
8587
88+
8689def stats (correct , wrong , answers ):
8790 print (" \n **** STATS ****\n " )
8891 print (" You answered" , len (correct), " questions correctly and" ,
8992 len (wrong), " questions wrong." )
93+
9094 if wrong:
9195 print (" These would have been the correct answers:" )
9296 for question in wrong:
9397 print (' ' , question, ' =' , answers[question])
9498```
9599
96- Let's try them out.
100+ Note that these functions have some empty lines in them and there are
101+ two empty lines between the functions. This makes the code a bit longer,
102+ but it's a lot easier to read this way.
103+
104+ Let's try out the functions.
97105
98106``` python
99107>> > answers = read_questions(' questions.txt' )
@@ -165,27 +173,33 @@ def read_questions(filename):
165173 answers[question.strip()] = answer.strip()
166174 return answers
167175
176+
168177def ask_questions (answers ):
169178 correct = []
170179 wrong = []
180+
171181 for question, answer in answers.items():
172182 if input (' %s = ' % question).strip() == answer:
173183 print (" Correct!" )
174184 correct.append(question)
175185 else :
176186 print (" Wrong! The correct answer is %s ." % answer)
177187 wrong.append(question)
188+
178189 return (correct, wrong)
179190
191+
180192def stats (correct , wrong , answers ):
181193 print (" \n **** STATS ****\n " )
182194 print (" You answered" , len (correct), " questions correctly and" ,
183195 len (wrong), " questions wrong." )
196+
184197 if wrong:
185198 print (" These would have been the correct answers:" )
186199 for question in wrong:
187200 print (' ' , question, ' =' , answers[question])
188201
202+
189203def main ():
190204 filename = input (" Name of the question file: " )
191205 answers = read_questions(filename)
0 commit comments