Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A quiz has two questions and two answers. Add a third question, What is the largest ocean?, with the answer pacific. How do you keep the lists in step?
Here are the two lists:
Questions and answers are two lists, so add to both.
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, compare your working with the solution.
Use append on both lists, in the same order:
questions.append("What is the largest ocean?")answers.append("pacific")Now both lists have 3 items, and position 2 holds the new question and its answer. If one append was forgotten, the loop would stop with an IndexError.
Log in to join the discussion.
Loading…