App Inventor and the Advanced Features


A Multiple Choice Quiz: How to work with the advanced features

Please see also the official documentation of the Any Component blocks.

It was asked in the forum How to create a multiple choice quiz with 4 possible answers displayed randomly on buttons.

This is a nice example to work with the advanced features, in this case the button advanced features. We also use a list of lists to store the multiple choice answers.

Designer

We are using a label to display the questions, 4 buttons inside a horizontal arrangement to display the answers and another label to display the result after game is over.

Blocks Editor

Definitions

We are using a list (listQ) to store the questions and a list of lists (listA) to store the answers. The first answer in the sublist is always the correct answer. Later of course (in procedure nextQuestion) we will assign the answers randomly to the 4 answer buttons...


Note: alternatively you could have defined the answers list of lists like this (I just wanted to save some space ;-)):

Screen Initialize


Note: after one of the latest updates you now also can define this directly in the definition of the list.

Next Question


Note: you can find the advanced button block in the Any component - Any button drawer.

Select an Answer


Note: you can find the advanced button block in the Any component - Any button drawer.

Game Over


In the procedure "gameover", the given answers are compared with the correct answers and the result is printed in a label.

How does this procedure work? It uses a for each loop to check each answer. in the example we have 3 questions, so the loop will run 3 times and check all 3 answers. The global variable "index" is the question number, on first run of the loop, the index is 1. The local variable "yourAnswer" selects the answer from listYourAnswer. The local variable "correctAnswer" selects the correct answer from listAnswer. A nested select list item block is used here: The inner select list item block selects the corresponding sublist (which are the 4 possible answers), the outer select list item block selects the first answer of that sublist, which is the correct answer. (Note: the correct answer is always the first item in the sublist.) Then in label lblResult the result is displayed.

Try again


For questions about App Inventor,
please ask in the App Inventor community.Thank you.

Screenshots

Questions and Answers

Q1: It was asked in the forum: I am planning on implementing a lives counter (starting with 3, wrong answers -1 life, 0 means back to title screen). Every time I press any button, the next question appears which is what I want but I just want to -1 life whenever a wrong answer is pressed as well. How do I connect any wrong answers with this proposed counter?
A: In the example the given answers are compared with the correct answers in procedure "gameover" for each question. You have to change the logic and check, after an answer was given for a question, if the answer is correct. I recommend you check that in procedure "selected" and adjust that procedure accordingly. To find out what to change, you should have a good understanding, how the example is working currently. The following 4 questions should help you to find the solution:
1. How do you get the current question number in procedure "selected"?
2. How do you get the given answer of the current question in procedure "selected"?
3. How do you get the correct answer of the current question in procedure "selected"?
4. How can you compare, if the given answer of the current question is correct or wrong?
If the answer was wrong, decrement the lives counter. Then check, if the lives counter = 0. If yes: "Game Over", else continue with the next question.

Here are the answers to these 4 questions:
1. the current question number can be found in the global variable "index"
2. the current answer is the text of the button component
3. the correct answer you get from the answer list (same as in procedure "gameover")
4. use an if statement

Screenshot of the modified procedure "selected"

For those, who do not like to "fish" themselves, try SteveJG's fish here. Thank you SteveJG!

The Correct Answers

Here are links to the correct answers, just in case...

  1. What is the answer to the Ultimate Question of Life, the Universe and Everything?
  2. How many toes and fingers altogether does a two-toed sloth have?
  3. In which year is Judgment Day, according to the movie 'Terminator 2'?

Download


Developing and maintaining snippets, tutorials and extensions for App Inventor takes a lot of time.
I hope it saved some of your time. If yes, then you might consider to donate a small amount!

Donation amount:

or donate some mBTC to Bitcoin Address:
1Jd8kXLHu2Vkuhi15TWHiQm4uE9AGPYxi8
Bitcoin

Thank you! Taifun
 

Download aia file for App Inventor
Back to top of page ...

Creative Commons License
This work by Pura Vida Apps is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
with attribution (name=Pura Vida Apps and link to the source site) required.


Home | Snippets | Tutorials | Extensions | Links | Search | Privacy Policy | Contact