Project: Quiz with a score
עמוד זה דורש דפדפן עדכני (תומך ב-SharedArrayBuffer). אנא עדכן את Chrome, Edge, Firefox או Safari לגרסה העדכנית ביותר.
English
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.
English
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
English
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)
English
names = ["Ann", "Ben"]
ages = [15, 16]
for i in range(len(names)):
print(names[i], "is", ages[i])
English
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.
English
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.
English
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().
לחץ על הרץ כדי לראות את התוצא כאן.