Planning a Quiz App

Need to decide whether/where to integrate pause all stuff lost with optionalization of mandala design and also to update debugging page. --MF, 7/5/19

On this page, you will begin to develop a quiz app by creating an abstract data type to pair the questions with their answers.

  1. Talk with Your PartnerDecide what type of quiz you would like to build, and write three to five questions and their correct answers.
  2. Take turns speakingRead the following section, clicking all the links and carefully reading all the code as you go. (Don't build these in Snap!. You'll build your own quiz later on the page.)

You'll use a list to store your quiz items in a variable. You could just put all the questions and their answers in a list like this and then use item () of 'list input slot' to retrieve each question and each answer using its index.

set (computer science quiz) to (list (What is the computer science term for looping or repetition?) (iteration) (What is the name for the abstraction in a program that can hold a value?) (variable) (What kind of variable is available only in the part of the program where it is defined?) (local variable))
That might be ok for the first quiz item (you could use item (1) of (computer science quiz) to get the first question and item (2) of (computer science quiz) to get the first answer), but for a long quiz, you might leave out one answer by accident and then after that, the program will get questions and answers mixed up.

: Sublist

A sublist is a list as an item of another list.

Instead, you can use a sublist for each question/answer pair. Then you could choose just one of those lists at a time to work with.

set (computer science quiz) to (list (list (What is the computer science term for looping or repetition?) (iteration)) (list (What is the name for the abstraction in a program that can hold a value?) (variable)) (list (What kind of variable is available only in the part of the program where it is defined?) (local variable))) ask (item(1) of (item (3) of (computer science quiz))) and wait
if (item (1) of (item (3) of (computer science quiz)) = answer) {
    say (That's correct!) for (2) secs
}

AAP.1.D.3

But with nested lists (lists with sublists) it can be hard to keep track of the input numbers for item of, so a better way is to use abstraction to organize the quiz items.

set (computer science quiz) to (list (question: (What is the computer science term for looping or repetition?) answer: (iteration)) (question: (What is the name for the abstraction in a program that can hold a value?) answer: (variable)) (question: (What kind of variable is available only in the part of the program where it is defined?) answer: (local variable))) ask (question of quiz item (item () of (computer science quiz))) and wait
if (answer from quiz item (item () of (computer science quiz)) = answer) {
    say (That's correct!) for (2) secs
}

AAP.1.D.2, AAP.1.D.4

This abstraction just hides the list and item of blocks, so it isn't complicated to build, but it can make your code much easier to write, read, and debug.

: Data Types
: Abstract Data Types The constructor and selector together implement the quiz item abstract data type.
  1. "U2L2-Quiz"Start a New Project called U2L2-Quiz
  2. AAP-1.D (Note that the exercise number has changed! -bh)
  3. Build the custom quiz item abstract data type (both the constructor and the two selectors).
  4. Specifying an Input Type

    Your selectors expect a quiz item, i.e., a list, as input. You can make your blocks show what type of data they expect. It's not necessary in Snap! but, like assigning a color to a block, it can be a helpful reminder of what the block does and what type of input it expects. You've already seen input slots of several shapes, indicating different expected data types.

    In the Block Editor while creating a selector, click on a plus sign to enter an input name. Then...

    1. Click on the arrow to the right of the input name:
      create input name right arrow
    2. Choose the data type you want for that input. (For this project, you'll use the "text" and "list" input types.)
    3. Click OK.

  5. Create a global variable to store your quiz items and initialize it as a list of items, using your constructor where appropriate.
  6. Save your work
  7. Test both the selectors for different items in your list of quiz items, and debug any problems.
  8. Imagine you make a variable capitals and use set to give this list of lists a name:
    set (capitals) to {{Augusta, Maine}, {Boise, Idaho}, {Columbia, South Carolina}, {Des Moines, Iowa}}
    Which of the following statements are true?

    Choose all that apply.

    item (1) of (capitals) reports a list with two items: "Augusta" and "Maine".
    item (1) of (item (1) of (capitals)) reports the word "Augusta".
    item (last) of (item (2) of (capitals)) reports the word "Iowa".
    length of (capitals) reports the number 8.
    all but first of (item (last) of (capitals)) reports a list with one item: "Iowa".