@@ -74,26 +74,34 @@ and then testing and fixing. Here are my versions of them:
74
74
def ask_questions (answers ):
75
75
correct = []
76
76
wrong = []
77
+
77
78
for question, answer in answers.items():
78
79
if input (question + ' = ' ).strip() == answer:
79
80
print (" Correct!" )
80
81
correct.append(question)
81
82
else :
82
83
print (" Wrong! The correct answer is %s ." % answer)
83
84
wrong.append(question)
85
+
84
86
return (correct, wrong)
85
87
88
+
86
89
def stats (correct , wrong , answers ):
87
90
print (" \n **** STATS ****\n " )
88
91
print (" You answered" , len (correct), " questions correctly and" ,
89
92
len (wrong), " questions wrong." )
93
+
90
94
if wrong:
91
95
print (" These would have been the correct answers:" )
92
96
for question in wrong:
93
97
print (' ' , question, ' =' , answers[question])
94
98
```
95
99
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.
97
105
98
106
``` python
99
107
>> > answers = read_questions(' questions.txt' )
@@ -165,27 +173,33 @@ def read_questions(filename):
165
173
answers[question.strip()] = answer.strip()
166
174
return answers
167
175
176
+
168
177
def ask_questions (answers ):
169
178
correct = []
170
179
wrong = []
180
+
171
181
for question, answer in answers.items():
172
182
if input (' %s = ' % question).strip() == answer:
173
183
print (" Correct!" )
174
184
correct.append(question)
175
185
else :
176
186
print (" Wrong! The correct answer is %s ." % answer)
177
187
wrong.append(question)
188
+
178
189
return (correct, wrong)
179
190
191
+
180
192
def stats (correct , wrong , answers ):
181
193
print (" \n **** STATS ****\n " )
182
194
print (" You answered" , len (correct), " questions correctly and" ,
183
195
len (wrong), " questions wrong." )
196
+
184
197
if wrong:
185
198
print (" These would have been the correct answers:" )
186
199
for question in wrong:
187
200
print (' ' , question, ' =' , answers[question])
188
201
202
+
189
203
def main ():
190
204
filename = input (" Name of the question file: " )
191
205
answers = read_questions(filename)
0 commit comments