Attempt to remove item 13 of a list of length 12 (English_Answers's List)

Hi! I am making a quiz app for a school project.
When I try my app in the MIT Companion, it shows me an error: "Attempt to remove item 13 of a list of length 12". I have revised the code a lot of times but I can't find any error. Could you help me, please?
This is my code:

can you attach the aia ?

APLICACIOTR(1).aia (7.5 MB)
Here you are, thank you for answering.

Welcome Ranya.

You do this a lot

remove

without first checking whether the list is empty or verifying that there are as many items in the List as the global Index you remove so you probably introduce a problem. You attempt to remove item 13 from a List that currently contains 12 items. :cry:

Which remove list item list Block is tripping the error? One of them; you can find out by deleting a remove list item one at a time from a copy of your Project until you discover the culprit. Or you can use an If statement to check if the particular List has sufficient items to honor your global Index value.

Hello Steve!
I have checked that and I have found out that the 4 remove list item list Blocks trip the error. (?)
Another thing which I don't understand is that if all my lists have 36 items, why the error messages say that my list has less items?
For example, if I generate 4 questions, my list should have 32 items (because all the items are removed after being picked), but it doesn't match the error message.

Possibly because every time you remove an item, the list's size (number of items) is decreased by one? You might read section 4 in List Blocks On App Inventor | imagnity

Perhaps that is not happening; you only may think you are doing that. Do you know how to debug using DoIT ? See either (or both) Enis’s advice ( http://twodogapps.com/?page_id=686 ) Taifun’s tip #4 https://puravidaapps.com/learn.php DoIt is a debugging tool that is part of App Inventor.

A working quiz app you might use as a model App Inventor Tutorials and Examples: Advanced Features | Pura Vida Apps

I have put a label which shows the length of the list of questions in order to see if it decreases by one, but something rare happens:
36,32,30,21,16,10,9,5,error.
These numbers are the length of the list when I generate a new question. As you can see, it doesn't decrease by one, it makes something strange. I can't find the problem.
Surely this fact causes the error which I said before ("Attempt to remove item 13 of a list of length 12")
I would appreciate it a lot if you can help me.

Hello!
My app consists in a quiz, so it has a list of questions. I need to remove each question when it's generated in order to avoid that it can appear once again. If I do that, the list should decrease by one when a question is generated.
I have put a label which shows the length of the list of questions in order to see if it decreases by one, but something rare happens:
36,32,30,21,16,10,9,5,error.
These numbers are the length of the list when I generate a new question. As you can see, it doesn't decrease by one, it makes something strange. I can't find the problem.
Surely this fact causes an error which doesn't let the app to run properly ("Attempt to remove item 13 of a list of length 12").
You have the aia to see my blocks (see Screen 3) and a screenshot.
APLICACIOTR (1).aia (7.5 MB)

I would appreciate it a lot if you can help me.

Can you attach a small aia with only 1 screen for testing?

I don't know how to generate an aia for only one screen.
I attach an aia with 2 screens (I can't remove screen 1).
small_aia.aia (7.5 MB)

Your small aia has a intro video no stop and many many images

reducedapp.aia (7.5 MB)
Try now
Sorry, I'm new at App Inventor and I don't know very well how it works.

Removing items from a list is tricky, and it gets even trickier if that list has to synchronized with other lists of the same length.

You can avoid that complexity by keeping a separate list (initially empty) of the indices (1-13) of the questions that have already been chosen. This approach involves no deletions from any lists.

There is a list block that can test if a number is in a list.
Each time you pick a random index from 1 to length of list(questions), put that index in a variable and test if that value is in the list of already chosen indices.
If it is in the list, go back and try another random selection, otherwise add it to the list of already chosen indices and use it.

Take care not to get caught in an infinite loop if you have already chosen all the available indices. (compare list lengths.)

Thank you for your help, ABG.
I have done what you said to me and now it works better, but it still appears an error: Pick random item: Attempt to pick a random element from an empty list.
I don't understand why the list gets empty after 4 questions if there are 36 items (it should get empty after 36 questions).
Uploading: blocks (2).png...
Screen4TR.aia (7.6 MB)

I think that this message appears when a repeated question is generated.
If a repeated question is generated, it should start again the procedure of "Generate Question", it shouldn't appear that error message.

Here is another way to avoid duplicates, using value procedures.
These blocks are draggable directly from this post to your blocks editor.

This value procedures returns a list of all index numbers [1,2,3,...length of list(a question list)]. It will be used in GenerateQuestion.

This value procedure takes two lists, and returns the difference between them.

This is your GenerateQuestion procedure, revised to only pick unused question indices.
It is immune to duplicated questions, since it only works by index, not by value.
global QuestionIndex

This is your EdgeReached event, made more bullet proofed:

  • Check if questions have been exhausted FIRST.
  • Use length of list instead of hardwiring a number (13)
  • Use greater than or equal as a limit test for extra protection
  • Removed duplicate detection code, since it is handled in the GenerateQuestion procedure.
    Screen4TR (1).aia (7.6 MB)

Thank you very much!!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.