Dr Logo for the Tatung Einstein, showing a rosette drawn by the turtle
← Back to Courses
module
25

A quiz

Introduction

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.

Asking one question

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.

Asking several

make "score 0 first, or the very first :score + 1 has nothing to add to.

The code

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

the quiz being played, two right and one wrong

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.

Change one thing

  • Answer with a capital: Paris. What happens, and why is that harsh? (Think about what = is comparing.)
  • Add a fourth question whose answer is two words, and answer it. Does it still work?
  • Move 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?
  • Take out make "score 0. Run quiz twice in a row and watch what the score does.

When it goes wrong

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.

Summary

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.

Next

S26, finding out what went wrong - the one you will come back to.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.