Project: Quiz with a score
Project: Quiz with a score
- You build a quiz: it asks questions, checks the answers and gives a score.
- The questions and the answers live in two lists.
- You use a
forloop,input(),if/elseand a counter.
What you will build
2 + 2 = ?
4
Right!
Capital of France?
London
Wrong, it is Paris
Colour of the sky?
blue
Right!
Score: 2/3
Step 1: two lists that match
questions[0]goes withanswers[0],questions[1]withanswers[1], and so on.range(len(questions))gives the positions0,1,2.- So one loop can use the same position
iin both lists.
![questions[i] goes with answers[i]; the quiz checks positions 0 to 2 and scores 2 out of 3](/static/code/python-basics/quiz-lists.png)
names = ["Ann", "Ben"]
ages = [15, 16]
for i in range(len(names)):
print(names[i], "is", ages[i])
Step 2: keep the score
- Start
score = 0before the loop. - Add
1inside the loop, only when the answer is right. - Print the score after the loop, once.
Step 3: forgive the typing
.strip()removes spaces at the start and the end:" paris ".strip()is"paris"..lower()makes everything small letters:"BLUE".lower()is"blue".- Compare the cleaned reply with the answer in small letters.
Now build it
- Three steps. Run it and answer the questions yourself, then press Check answer.
- Want more? Add your own questions to both lists.
Сложность 3/5 · 200 монет + небольшой бонус за первое завершение сегодня
Step 1: for each question, print it, read the answer with input(), then print Right! или Wrong, it is + the right answer. For the answers 4, London и blue, the second question prints Wrong, it is Paris.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 3/5 · 200 монет + небольшой бонус за первое завершение сегодня
Step 2: count the right answers. After the last question, print the score like Score: 2/3 (right answers / number of questions).
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 3/5 · 200 монет + небольшой бонус за первое завершение сегодня
Step 3: be kind to typing. Count an answer as right even with different capitals or extra spaces, so paris и BLUE are right. Use .strip() и .lower().
Нажмите Запустить, чтобы увидеть результат здесь.