psychopy.org | Reference | Downloads | Github

TypeError: list indices must be integers or slices, not str

Windows 10
PsychoPy version V2020.1.3

What are you trying to achieve?:
I am trying to set the text to a word based on a counter.

Specifically, at the beginning I load some words from a spreadsheet (like here), into “wordlist” then I also set

> wordcounter = 0

(The column that I load into wordlist is called “word”)

Then I try to set the text by using

> text.setText(wordlist[word[wordcounter]])

What specifically went wrong when you tried that?:
The experiment runner keeps on giving me the error message, no matter what variation I try:

text.setText(wordlist[word[1]])
TypeError: list indices must be integers or slices, not str

text.setText(wordlist[word[wordcounter]])
TypeError: list indices must be integers or slices, not str

text.setText(wordlist[word])
TypeError: list indices must be integers or slices, not str

To answer my own question, which I found in the Brooke’s template, the proper syntax is more like

text.setText(wordlist[wordcounter][0])

or

$wordlist[$wordcounter][0]

If you’re using the coder gui, but be warned by the post below.

Don’t use two $ signs. They mean interpret this as code rather than this is a variable.