Page 3: Checking Each Quiz Answer

Unit 2, Lab 2, Page 3

On this page, you will complete your quiz app by using a new block that loops through a list:
for-each-(item)-of(){}

This should be an FYTD. Is is everywhere else. –MF, 5/30/20

If it’s not already open, open your project U2L2-Quiz.

The students are considering how to use their abstract data type as they develop the code for their project.

Alphie: We can use our question from quiz item: () and answer from quiz item: () selectors to get the question and answer for quiz item in our list. But how do we get the quiz item out of the list in the first place?

Gamal: Maybe we can use item of inside a for loop and use the index from the for loop as the first input to item of, like this:
for (i) = (1) to (4) {
ask (question from (item (i) of (computer science quiz))) and wait
}

Using the result from item as the input to address from contact is called composition of functions.

Betsy: Yeah, we could, but that’s a little hard to read with that index variable, i, to keep track of. And if we add more questions, we’ll have to update that number 4 that tells us when to end the loop. I bet we can use for each to traverse the list.

Betsy drags for each into the scripting area.
for each (item) of 'list input slot' {}

Gamal: We could even rename item something logical like quiz item. Then the code inside will run once for each quiz item in our list.

: Traversing a List

Traversing a list means looking at each item of the list. For each is iterative. That is, it’s repetitive, like for, which can also traverse a list. But unlike for, for each traverses the list without using index numbers.

  1. Use for each to traverse your list, asking each quiz question, checking each answer, and letting the user know if they are right or wrong on each item.
    Do not copy the code that Gamal did above
  2. Take your quiz a few times as a user would, and work out any problems with the code. Then, give your quiz to a friend.
  1. If the user is wrong, don’t just let them know, but tell them the right answer too.
  2. Use a script variable to keep track of the player’s score and report it at the end of the quiz.

The procedure

goto()

isn’t built in to the AP’s language so it is written in lower case like other programmer-defined procedures.

The script for each (point) of (my drawing) (go to (point)) would be written as

FOR EACH point IN myDrawing
{
goto(point)
}

or FOR EACH point IN myDrawing
{
goto(point)
}.

  1. Before telling the user the right answer, give them three tries to get it right.
  2. Users might give answers that are close enough but not exactly the answer you expected. For example, someone might answer “What kind of variable is available only in the part of the program where it is defined?” with “a local variable,” “local variable,” or just “local,” or the same possibilities for “script variable.” So your question answer ADT could take a list of keywords in the second slot and accept any answer that includes any of the keywords (so in this case, the list would be {local, script} and would accept “a local one”). Use 'list input slot' contains () to adapt your code.