Course 5.3Lesson 2 of 4
A quiz keeps its questions in one list and its answers in another, in the same order, so that the same position belongs together. A loop goes through the positions with range of len, asks each question, and compares the reply with the answer. The comparison is a small function that ignores capital letters and spaces at the ends. A second function gives the percentage. If the two lists are not the same length, the loop reaches a position that one list does not have and Python shows an IndexError. Test the function with several replies, and extend the quiz with more questions and messages.
Try the code and the simulations beside the lesson.
After this lesson you can
A quiz needs four things: questions, answers, a way to ask, and a score.
Work out the expected result first. The four questions of our quiz have the answers paris, 7, 144 and mars. If a student answers Paris, 7, 150 and Mars, three are right: 3 out of 4, which is 75 percent.
The most delicate part is comparing a reply with an answer. A student may type Paris, PARIS or paris . All three are right. The comparison must forgive capitals and spaces at the ends. It is a job for a small function, and we can test it before we build the quiz.
Type four answers in the Input box, one on each line, for example Paris, 7, 144 and Mars, and press Run. Then type wrong answers, and see the other message.
for i in range(len(questions)): goes through the positions 0, 1, 2 and 3.questions[i] and answers[i] are the matching pair.score is a counter. It starts at 0, and 1 is added for each correct reply.Python rounds a half to the even neighbour, so 87.5 percent shows as 88 and 62.5 percent as 62.
What if you add a question to questions and forget to add its answer to answers?
Test the function with the cases that matter: the right answer in a different style, a wrong answer, an empty reply, and a reply with spaces inside. Test the percentage for 0, for all right, and for a part.
| Test | Expected |
|---|---|
is_correct("PARIS", "paris") | True |
is_correct(" paris ", "Paris") | True |
is_correct("", "paris") | False |
is_correct("par is", "paris") | False |
percentage(0, 4) | 0.0 |
percentage(4, 4) | 100.0 |
Pick one or two, and do them without looking at a solution.
Keep practising. 50 to 79: Good. 80 and more: Excellent. This is a decision ladder, so test its edges.7 also accept seven. A list of allowed answers for each question is one way.random.shuffle() mixes a list. But the answers must be shuffled in the same way, so think about how to keep the pairs together.IndexError.lower() and strip(). The student who types Paris loses a mark.len(questions).input() in the function that compares. You cannot test it.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
for i in range(len(a_list)) goes through the positions.lower() and strip() before you compare. Put the comparison in a function and test it.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
is_correct before building the quiz?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.