psychopy.org | Reference | Downloads | Github

AttributeError: 'str' object has no attribute 'addData'

I’m making a basic stroop task test with only one routine with three components and a loop. There is a text component, a key response component and a code component to randomly determine the word displayed and the color of the word. The following code is used:

decide1 = randint(1, 3)
decide2 = randint(1, 3)

if decide1 == 1:
    color = 'red'
    answer = 'left'
    
elif decide1 == 2:
    color = 'blue'
    answer = 'right'
    
if decide2 == 1:
    word = 'Red'
    
elif decide2 == 2:
    word = 'Blue'

if color == 'red' and word == 'Red':
    congruent = 1
    
elif color == 'blue' and word == 'Blue':
    congruent = 1

else:
    congruent = 0

thisExp.addData('word', word)
thisExp.addData('color', color)
thisExp.addData('congruent', congruent)

The program begins, a randomly colored word appears, and the program crashes when a response is made on the keyboard. The following error message appears:

color.addData('color_word.started', color_word.tStartRefresh)
AttributeError: 'str' object has no attribute 'addData'

This is very confusing because I never made any object named ‘str’ and I assume it is made by psychopy itself. If anyone could help me out that would be greatly appreciated.

Hi @LelBartha. is this a piece of code you have added yourself? If not, would you please share your full error message?

This is the full error message. I uploaded a screenshot because I’m not sure how else to share it.

Thanks, have you named one of your loops as color?

Yes. The code component is called “color_code”, the text is “color_word” and the keyboard component is “color_resp”

Thanks, well if you have a loop called “color”, you should change it to something else, because you also have a variable called “color” for changing your text color, and this will overwrite the loop handler and cause the error.

1 Like

Thank you so much! Such a stupid mistake…