Mode debug help?

Hello,

I’m not great with code, and I’m not sure how to deal with this error. I appreciate any help anyone can offer.

I am versioning the Spanish Flash Card mode to Klingon phrases. The mode runs fine, but it does display an error in the info screen after a little while running. I’d like to understand the issue and fix it before sharing the mode.

To make this mode, I did 3 things:

  1. I made a version of the wordlist with Klingon words & phrases, “klingon.py”
  2. I changed the variable named “spanish_words” to “klingon_words” and updated the references to the wordlist
  3. I switched word1 and word2, so Klingon will display first, then English.

I’m getting this error after the mode has been running a little bit:

Traceback (most recent call last):
File “main.py”, line 203, in
mode.draw(screen,etc)
File “/usbdrive/Modes/T - Klingon Flash Cards/main.py”, line 36, in draw
word2 = unicode(words[word_index][1])
IndexError: list index out of range![KlingonModeError|666x500]

I’m attaching a jpg of the error in case I transcribed wrong.

Here is the mode: Klingon Flash Cards | Patchstorage

The Klingon phrase list is much shorter than the original Spanish one, it’s only 60 lines long. My guess is that the index out of range might have to do with this length. But, as far as I can see, the index in the code is pulled from the length of the wordlist. (I could be wrong, of course, I know just enough about code to be dangerous.)

Can anyone help me understand what is going on here?

Thank you.

ok, no time to check your patch,
but a quick look at Spanish words on GitHub (assuming its up to date)

Id say line 33 has a bug

            word_index = random.randint(0,len(words))

it should be

            word_index = random.randint(0,len(words) - 1)

the reason is the array is zero index, so if an array has 100 elements, it runs from 0 to 99, not 0…100

(theoretically there should also be a check for zero words in the array, but lets assume we know that’s not the case :wink: )

so why does Spanish words not throw the error, it probably would with time.
but if you reduce the size of the array (less words) then the probability it will choose the last (and incorrect) entry rises - so hence why you probably see it more often.

disclaimer, this is just a ‘guess’ based on a quick look at the Spanish words code, as didn’t have time to run anything, nor look at your code :wink:

1 Like

That did the trick. Thank you very much for your help, and for the detailed explanation - I appreciate it.

I’ll be sharing the mode on patchstorage shortly.

1 Like