Step 3: Loop the Questions ๐
Look at your code from Step 2. The same if/else block, copied three times. Want 10 questions? Copy it 10 times? No thanks. ๐ โโ๏ธ
Programmers have a rule: if you're copying, you should be looping.
Put the questions in one list and the answers in another, in the same order:
questions = ["Capital of France? ", "6 times 7? "]
answers = ["paris", "42"]
Question 0 goes with answer 0, question 1 with answer 1. Now loop over
the positions with range(len(questions)):
for i in range(len(questions)):
reply = input(questions[i]).lower().strip()
if reply == answers[i]:
print("โ
Correct!")
len(questions) is how many questions there are, so i counts
0, 1, 2... and questions[i] grabs the one at that position. One block
of code, as many questions as you like. ๐คฏ
Your mission
- Make a
questionslist and ananswerslist (at least 3 each) - Loop with
for i in range(len(questions)): - Keep the score going inside the loop
- Print the final score after the loop ends
โ ๏ธ Watch your answers: store them lowercase so .lower() matches!
Your code
Press โถ Run to see what your code does!