From Test-Scratch-Wiki
Scratch contains the resources available for creating a question/answer system. This system can be used for one to be "quizzed", or to repetitively answer automated questions. This tutorial shows various methods on how to create a question system. For each method, the ask () and wait block is used to ask questions and input answers.
Simple Method
This method can become lengthy in scripting, repetitive, and orderly, but still accomplishes a simple question system. The script tells the sprite to ask a question, detect if it is right or wrong, and play a sound according to the outcome. This is a simple way to program a quiz game.
when gf clicked ask [What is 1+1?] and wait if <(answer) = [2]> then play sound [correct! v] until done change [correct v] by (1) else play sound [wrong v] until done end . . . //repeat the above script with different questions for any desired amount of time
List Method
The next method continuously picks a random question to ask, with questions and answers added to certain lists. The list storage makes adding questions and answers quicker. Each item in the list "questions" must directly correspond to the same item in "answers". Further methods can be used to group questions and implement other functions for various purposes. For the following tutorial, assume the following:
- "questions" is the list which contains each question
- "answers" is the list which contains the corresponding answers to the questions in the list above
- "item#" is a variable used to determine the current question one can answer
when gf clicked forever set [item# v] to (pick random (1) to (length of [questions v)) //picks a random question ask (item (item#) of [questions v]) and wait //asks he question and waits for the response if <(answer) = (item (item#) of [answers v])> then//if the answer is correct play sound [correct! v] until done//notifies the viewer that the answer is correct else play sound [wrong v] until done//notifies the viewer that the answer is incorrect say (item (item#) of [answers v]) for (1) secs //used to display what the correct answer was
Group Method
This method is much like the List Method of coding a quiz, but the Grouping Method can be used to group questions and repetitively ask the specified group of questions until a condition is reached. For example, if there are twenty questions, this method can be used to ask four of those questions in the group until they are known very well, and then the system will select a new group. For this method, assume the additional following:
- "current questions" is the list which contains the current group of questions
- "current answers" is the list which contains the current group of corresponding answers
- "correct" is the variable used to determine how many questions of a specific group have been correct
when gf clicked forever delete (all v) of [current questions v] //clears the current group's questions delete (all v) of [current answers v] //clears the current group's answers set [correct v] to (0) //resets the amount if correct answers for the new group repeat ([ceiling v] of ((length of [questions v]) / (5)) //each group contains 1/5 of the total questions set [item# v] to (pick random (1) to (length of [questions v])) add (item (item#) of [questions v]) to [current questions v] //adds a questions to a group add (item (item#) of [answers v]) to [current answers v] //adds the corresponding answer end repeat until <(correct) = [15]> //"15" can be changed to the desired amount needed correct each group set [item# v] to (pick random (1) to (length of [current questions v)) //picks a random question ask (item (item#) of [current questions v]) and wait //asks he question and waits for the response if <(answer) = (item (item#) of [current answers v])> then//if the answer is correct play sound [correct! v] until done //notifies the viewer that the answer is correct change [correct v] by (1) else play sound [wrong v] until done //notifies the viewer that the answer is incorrect say (item (item#) of [current answers v]) for (1) secs //used to display what the correct answer was
Mathematical Method
This method is used in a math quiz in a way very much like the list method, but takes use of the Operators blocks to make several possibilities without needing to list them. Take, for example, randomly generated addition questions involving addends from 1 to 20. For this method, assume these variables:
- "number1" is the first added to the question
- "number2" is the second added in the question
- "expect_answer" is the expected answer of the generated question
when gf clicked forever set [number1 v] to (pick random (1) to (20)) set [number2 v] to (pick random (1) to (20)) // generates two random integers from 1 to 20 hide variable [expect_answer v] // hides the answer if it is still showing set [expect_answer v] to ((number1) + (number2)) // calculates the result ask (join (number1) (join [ + ] (number2))) and wait // asks the question if <(answer) = (expect_answer)> then // if the answer is correct play sound [correct v] until done// notifies the viewer that the answer is correct else // if the answer isn't correct play sound [wrong v] // notifies the viewer that the answer is incorrect show variable [expect_answer v] // tells viewer what the correct answer was wait (4) secs // assures that the viewer has enough time to react before the variable is hidden again by the next loop end end