Comparing list and string variables to return true

I am having issues comparing a list and a string variable. In a code component, I have a script where I assess if participants clicked in one of 4 images on a screen (item_1, item_2, item_3, item_4) and see if this image matches a variable in my conditions file known as “corrAns”

#Begin experiment
item_score = 0

#End Routine
if testMouse.clicked_name == ['item_1'] and corrAns == 'item_1':
    item_score == item_score+1
elif testMouse.clicked_name == ['item_2'] and corrAns == 'item_2':
    item_score == item_score+1
elif testMouse.clicked_name == ['item_3'] and corrAns == 'item_3':
    item_score == item_score+1
elif testMouse.clicked_name == ['item_4'] and corrAns == 'item_4':
    item_score == item_score+1
else:
    item_score == item_score+0

thisExp.addData("ItemScore", item_score)

In the output “ItemScore” always ends up as 0. I tried debugging to explore what type testMouse.clicked_name and corrAns come out as. Clicked_name produces a list (e.g., [item_1, item_1]) whereas corrAns is string.

I suspect this might be the issue when I am comparing clicked_name to corrAns. Is there anyway to compare a list and string so that it can return “true” when they match?

I started looking at this previous discussion which helped me get started:

Hi There, you might need to use testMouse.clicked_name[-1] (because I think clicked_name could be a list ?)

Thanks,
Becca

Hi Becca!

Thanks for the suggestion. I had a little play around with what you suggested (you are right - it produces a list!) and managing to get everything working. For anyone else that might have a similar problem, below is the code that worked for me in the end. Although please note I also had some mistakes “==” rather than “=” at times.


if testMouse.clicked_name[0] == 'item_1' and corrAns == 'item_1':
    item_score += 1
elif testMouse.clicked_name[0] == 'item_2' and corrAns == 'item_2':
    item_score += 1
elif testMouse.clicked_name[0] == 'item_3' and corrAns == 'item_3':
    item_score += 1
elif testMouse.clicked_name[0] == 'item_4' and corrAns == 'item_4':
    item_score += 1
else:
    item_score += 0