Having trouble understanding why this error occurs

I am trying to have a question from a data table appear in a form
The question is chosen by the random function and there are dozens of questions

 def continue_button_show(self, **event_args):
    This method is called when the Button is shown on the screen"""
    self.my_list = list(app_tables.questions_choice.search())
    random.shuffle(self.my_list)
    self.index = 0
    self.label_14.visible = True
    
  def update_question(self):   
      row = self.my_list[self.index]                            # This is line 27
      self.label_1.text = row['question_column']
      self.label_3.text = row['Column1']
      self.label_4.text = row['Column2']
      self.label_5.text = row['Column3']
      self.label_6.text = row['Column4']
      self.label_7.text = row['Column5']
      self.label_8.text = row['Column6']      
      self.label_9.text = row['Column7']
      self.label_10.text = row['Column8']

The column from which the questions are being asked has already been accessed earlier in the code and this code is meant to prevent (or at this stage highlite the fact that the random choice has already been used).I then intend writing some code to skip that question and re-run random choice.

 def continue_button_click(self, **event_args):
   """This method is called when the button is clicked"""   
   try:
     row = self.my_list[self.index]      
     q = row['question_index']
     self.log_second_answers(q)
     self.index +=1
     
   except Exception as e:
     print (e)  
     
   for row in app_tables.button_capture_from_user.search():
     existing_question_numbers = (row["question_number"])
     row = self.my_list[self.index]      
     q = row['question_index']
     if q == existing_question_numbers:
       print ('found one')

   for row in app_tables.button_capture_from_user.search():
     existing_question_numbers = (row["question_number"])
     if q != existing_question_numbers:
       print('ok')
       self.update_question_index()
     else:
       q == existing_question_numbers
       print ('found one')

The program runs perfectly for some time and finds the repeated questions then continues perfectly (approximately 4 to 8 cycles)and then this error appears.The error does not appear after a constant amount of questions asked.

This is the eroor that appears
IndexError: list index out of range at [Homeform.questionForm.questionForm_3, line 27](javascript:void(0)) called from [Homeform.questionForm.questionForm_3, line 73](javascript:void(0)) called from [Homeform.questionForm.questionForm_3, line 63](javascript:void(0))

This error occurs when the index you are trying to access in the list is not with in the list. Or trying to access index 5 when the list only has four elements. Or the list is empty therefor any index will be outside the range of the list. Make sure the list is not empty before indexing it.

Which line does the error relate to?

Al

Hi Rick
Thanks for the response
The line self.my_list = list(app_tables.questions_choice.search())
shows the place the list is getting populated from and there are 100 items in the table
I understand if there are no items to search the error will appear but it seems to me there is something causing the list to appear with more items than are actually there with regard to indexing.As I mentioned the number of times the list is referenced before the error appears varies.
I have printed out the index number when the error occurs and it is always greater than the number of items in self.my_list = list(app_tables.questions_choice.search())
so I get it that the index is out of range but what I can’t understand is how is it possible for the random generator to get an index out of range?

solution found
Although there are more than 100 rows in the table when I select load more,only up to 100 rows are available to be randomly selected.

The 100 row limit you see in the data table view before clicking load more is just for the IDE and shouldn’t affect the search iterator.

Remember that a list of 100 items has a max index of 99 since the first element is actually at index 0

Can you post a clone link?

1 Like

If this was me I would refactor all of your code using sets.

I would use the set.pop() method to randomly display 12 (or any number) of items, and add the items to a new set if you wanted to keep track of items already displayed.

1 Like

It is hard to understand your question without any clone link. I can see that you are using random.shuffle() method in your code but are you also using random.choice() somewhere? Because if you pass an empty list to random.choice(), it can return the error above.

1 Like