Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A quiz has 5 questions but only 4 answers. What happens when the loop for i in range(len(questions)): runs, and what is the message? Write one line that protects the program.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. The loop uses the positions 0 to 4 of questions. At position 4, it asks for answers[4], but the list answers has only the positions 0 to 3.
Python stops with IndexError: list index out of range, after the four questions that had answers.
The protection. Check the lengths before the loop.
Check. Run it. The message shows 5 and 4. After you add the missing answer, nothing is shown.
Answer: An IndexError at the fifth question. Compare len(questions) with len(answers) before the loop.
Log in to join the discussion.
Loading…