Kids Coding
Project: Scored Quiz
Questions in a list of dicts. Track score. Print a result you can explain.
KidsAdvancedProject: Scored Quiz
Questions as data
Store each question as a dict. Loop the list. Compare answers. Keep a score. Now adding a question is one more dict, not a new if block.
Quiz from a table
questions = [
{"prompt": "2 + 2?", "answer": "4"},
{"prompt": "Colour of the sky?", "answer": "blue"},
{"prompt": "A sly animal?", "answer": "fox"},
]
given = ["4", "green", "fox"]
score = 0
for i, item in enumerate(questions):
print(item["prompt"])
print("You said:", given[i])
if given[i] == item["answer"]:
print("Correct")
score = score + 1
else:
print("It was", item["answer"])
print()
print("Score:", score, "/", len(questions))Check the answer first, then add to the score once. A misplaced += 1 is why a quiz sometimes gives 6 out of 5.
Real life
A spelling test: the questions live on a sheet, the ticks live in a score.
Put the questions in data. The loop is the test. Adding a word is one more dict.
Spelling test
words = [
{"ask": "cat", "answer": "cat"},
{"ask": "fox", "answer": "fox"},
]
given = ["cat", "fos"]
score = 0
for i, item in enumerate(words):
if given[i] == item["answer"]:
score = score + 1
print("Spelling:", score, "/", len(words))Change one word. Run it. That is how this idea shows up outside the lesson.
Try it
Exercise 1: Make a list with one question dict. If given matches answer, print ok.
Show solution
item = {"prompt": "1+1?", "answer": "2"}
given = "2"
if given == item["answer"]:
print("ok")