Breaking a problem into smaller steps
“Make a quiz” sounds like one task, but it contains several smaller tasks. Splitting a large problem into parts is called decomposition. You can work on one part without holding the whole project in your head.
Build it like a model
When building a model house, you might make the walls, roof, and door separately. For a quiz, useful parts are the question, the answer, the check, and the score. Draw four boxes on paper and connect them in that order.
Start with one question. A menu, music, and ten difficulty levels can wait until the basic quiz works. Smaller tasks also make it easier to tell where a bug lives.
A one-question quiz
This example needs a runner that supports text questions. Enter 4 when asked.
# language: en
let score = 0
let answer = ask("What is 2 + 2?")
if answer == "4":
set score = score + 1
say "Correct!"
else:
say "Try again next time."
say "Score:", score
The first two lines prepare the score and get an answer. The if decides which message to show. Finally, the program reports the score. ask returns text, so this example compares its answer with the text "4".
Predict, then run
What should happen with the answer 4? What should happen with 5?
For 4, the output is:
Correct!
Score: 1
For 5, the program reports zero points and the other message. Both paths need checking; one successful answer does not test the whole quiz.
Your challenge
Write a second question on paper. Identify its question, expected answer, feedback, and scoring rule before adding any code. Test the first question again after adding the second.
A common mistake
Adding many new parts at once makes problems harder to locate. If the quiz works and the new music does not, keep the working quiz and investigate the sound part. Later, functions can give reusable parts their own names.
