Checking Each Quiz Answer

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
AAP-2.O.2

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.

    AAP-2.K part a, AAP-2.O part a
  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.
  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.
    AAP-2.K part b
  1. Which of the scripts below will produce the following sprite behavior?
    sprite saying Abraham then Lincoln
    (When you run the script, though, it actually says Abraham Lincoln only once.)

    Choose all that apply.

    for each (item) of {Abraham, Lincoln} [say (item) for (1) secs]
    say {Abraham, Lincoln} for (2) secs
    say (item (1) of {Abraham Lincoln, George Washington, Barack Obama}) for (2) secs
    say (item (1) of {Abraham, George, Barack} for (1) secs, then say (item (1) of {Lincoln, Washington, Obama} for (1) secs
AAP-2.O.3
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)
}.

These questions are similar to those you will see on the AP CSP exam.

  1. AAP-2.O part b
    inputList ← [3, -1, 2, 10, -5]
    
    FOR EACH item IN inputList
    {
      IF(item > 0  AND  item * item > 4)
      {
        DISPLAY(item)
      }
    }
    

    What will be displayed as a result of running the code segment given?
    9, 1, 4, 100, 25
    9, 100
    3, 2, 10
    3, 10
  2. PROCEDURE Mystery(numberList, targetNumber)
    {
      counter ← 0
      FOR EACH number IN numberList
      {
        IF(number > targetNumber)
        {
          counter ← counter + 1
        }
      }
      RETURN(counter)
    }
    myMathGrades ← [100, 80, 90, 80, 60, 100, 50, 100]
    Mystery(myMathGrades, 60)
    

    What will be returned if the code above is run?
    [100, 80, 90, 80, 100, 100]
    7
    6
    60
  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.