
Everything is in place now. You can ask a question, read what comes back, decide whether it is right, remember a number, and say something about it. That is a quiz.
This is the longest program in the course and it is still nine lines.
to ask :question :answer
pr :question
if rl = :answer [pr [right] make "score :score + 1 stop]
pr se [no - it was] :answer
end
Read the middle line carefully, because it is the whole program.
rl waits for an answer and gives back a list. :answer is a list too -
that is why the calls below pass [paris] and not "paris. = compares two
lists happily, so rl = :answer is TRUE when what was typed matches what was
expected.
The instruction list holds three instructions: say so, add one to the score,
and stop. An if list is not limited to one.
And stop leaves ask - not the whole quiz. That is what makes the last line
work: it only ever runs when the answer was wrong, because a right answer
stopped before reaching it. No else needed.
to quiz
make "score 0
ask [what is the capital of france?] [paris]
ask [how many sides has a hexagon?] [6]
ask [what is 7 times 8?] [56]
pr se [you scored] :score
end
make "score 0 first, or the very first :score + 1 has nothing to add to.
to ask :question :answer
pr :question
if rl = :answer [pr [right] make "score :score + 1 stop]
pr se [no - it was] :answer
end
to quiz
make "score 0
ask [what is the capital of france?] [paris]
ask [how many sides has a hexagon?] [6]
ask [what is 7 times 8?] [56]
pr se [you scored] :score
end
quiz
Starting from
A fresh boot, at the ? prompt.
What you should see
?quiz
what is the capital of france?
paris
right
how many sides has a hexagon?
square
no - it was 6
what is 7 times 8 ?
56
right
you scored 2

Note the space in 7 times 8 ? that you did not type. 8? is a number followed
by punctuation, which Dr Logo reads as two things in the list; france? is
a single word, so it printed unchanged. Lists hold elements, not spacing - S15
said so, and here is what it looks like.
Paris. What happens, and why is that harsh? (Think
about what = is comparing.)stop out of the if list and put pr se [no - it was] :answer inside
an if of its own instead. Same behaviour, more lines - which would you
rather read in six months?make "score 0. Run quiz twice in a row and watch what the score
does.| Symptom | Cause |
|---|---|
score has no value |
No make "score 0 before the first ask. |
| Every answer is wrong | The expected answer was passed as a word - "paris - and rl gives a list. Use [paris]. |
| A right answer still scores nothing | make is outside the if list. Everything that should happen on a right answer goes inside the brackets. |
| It stops after the first question | stop is in quiz rather than in ask, so it leaves the whole thing. |
| The score keeps climbing between runs | make "score 0 is missing, so the old value is still there. |
rl gives a list and = compares lists, so checking an answer is one
comparison. An if list can hold as many instructions as you need, and stop
inside it leaves only the procedure it is in - which is what lets a right answer
skip the rest without an else.
S26, finding out what went wrong - the one you will come back to.