Project: Quiz with a score
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
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.
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.
Step 1: for each question, print it, read the answer with input(), then print Right! or Wrong, it is + the right answer. For the answers 4, London and blue, the second question prints Wrong, it is Paris.
Click Run to see the output here.
Step 2: count the right answers. After the last question, print the score like Score: 2/3 (right answers / number of questions).
Click Run to see the output here.
Step 3: be kind to typing. Count an answer as right even with different capitals or extra spaces, so paris and BLUE are right. Use .strip() and .lower().
Click Run to see the output here.