What it shows
A dictionary stores questions with their answers, a for loop asks them one by one, and an if statement compares answers. It is the first full program most students write.
The science
A dictionary stores pairs of a key and a value, here a question and its answer. The for loop with the items method gives one pair at a time. The input function reads typed text, and strip and lower clean it so that capital letters and extra spaces do not make a right answer wrong. A variable named score counts the right answers.
Materials
Cost: free. Many items come from home or from waste; spare parts such as LEDs, motors and wire are in the store.
- A computer, laptop or phone with a browser (free)
- Python 3 with IDLE, or the free Thonny editor, or an online Python runner (all free)
- A notebook and a pencil
Safety
This project uses safe, everyday materials. Still, read the rules below with a parent before you start.
Never use mains electricity: power models with cells, batteries or a low-voltage adapter. Sharp and hot tools, flames and lab chemicals need an adult or your teacher beside you. Keep magnets, button cells and small parts away from young children, and wash your hands after making.
All the safety rulesStep by step
8 steps. Do them in order, and test each part before you go on to the next. Write what you notice in a notebook as you go.
Open IDLE or Thonny, make a new file and save it as quiz.py.
Write the questions: quiz = {"Capital of India?": "new delhi", "National animal of India?": "tiger", "How many days are in a leap year?": "366"}.
Add score = 0 on a new line and print a welcome line with print("Welcome to the quiz").
Start the loop: for question, answer in quiz.items(): followed by an indented line reply = input(question + " ").
On the next indented line clean the reply: reply = reply.strip().lower().
Add if reply == answer: with indented print("Correct") and score += 1, then else: with print("Wrong, the answer is", answer).
After the loop (no indent) write print("You scored", score, "out of", len(quiz)).
Run it with F5 in IDLE or the Run button in Thonny and add five more questions of your own.
What you should see
The program asks each question, says Correct or shows the right answer, and prints the final score out of the number of questions.
Viva questions
A teacher or a judge will ask how it works. Try to answer each question before you open it, then compare.
1What is a dictionary?
A collection of key and value pairs, such as a question and its answer.
2Why use lower() on the reply?
So that Tiger and tiger are treated as the same answer.
3What does == do?
It compares two values and gives True if they are equal.
4Why does indentation matter in Python?
It shows which lines belong inside the loop or the if.
Take it further
Shuffle the questions with random.shuffle on a list of the keys so that each game has a different order.
Change one thing, measure what happens, and add a table or chart of your readings: that is what turns a model into your own investigation.
Free ebooks for this project
Computer & office
Computer Fundamentals Study Book
The complete study notes, practice questions and glossary for Computer Fundamentals
