psychopy.org | Reference | Downloads | Github

Learning task correct responses

I’m creating a learning task where participants need to select the correct image out of two images. I have a column in the excel spreadsheet named CorrAns defining which image is correct.

I’m trying to do two things:

  1. Record whether the image selected was correct or not. To do this I’ve put the following code in the end routine tab:
if mouse.clicked_name=='image1':
    if CorrAns=='Image1':
        thisExp.addData('correct', 'Yes')
elif mouse.clicked_name=='image2':
    if CorrAns=='Image2':
        thisExp.addData('correct', 'Yes')
else:
    thisExp.addData('correct', 'No')

However, even when the correct image is selected it records it as No in the correct column.

  1. Give feedback depending on if response was correct or not. To do this I’ve put the following code in a new routine, in the begin routine tab:
 if mouse.clicked_name=='image1':
    if CorrAns=='Image1':
        Feedback='you WIN 100 points'
elif mouse.clicked_name=='image2':
    if CorrAns=='Image2':
        Feedback='you WIN 100 points'
else:
    Feedback='you LOSE 100 points'

But even when the correct response is selected I still get the else feedback ‘you LOSE 100 points’.

Any help on what I’m doing wrong here would be greatly appreciated.

Try inserting some (temporary) debugging code so you can see exactly what is happening on each trial and why the comparisons are failing:


print(mouse.clicked_name) # DEBUG
print(type(mouse.clicked_name)) # DEBUG
print(CorrAns) # DEBUG

if mouse.clicked_name=='image1':
    if CorrAns=='Image1':
        thisExp.addData('correct', 'Yes')
elif mouse.clicked_name=='image2':
    if CorrAns=='Image2':
        thisExp.addData('correct', 'Yes')
else:
    thisExp.addData('correct', 'No')

That logic could just be folded into the code above, so both the data recording and the message-setting happen at the same place - that way there is less to go wrong and you can’t get inconsistent results.

Hi Michael, thank you for the reply. I’ve done what you’ve suggested with the following code:

if mouse.clicked_name=='image1' and CorrAns=='Image1':
    thisExp.addData('correct', 'Yes')
    Feedback='you WIN 100 points'
elif mouse.clicked_name=='image2' and CorrAns=='Image2':
    thisExp.addData('correct', 'Yes')
    Feedback='you WIN 100 points'
else:
    thisExp.addData('correct', 'No')
    Feedback='you LOSE 100 points'

print(mouse.clicked_name) # DEBUG
print(type(mouse.clicked_name)) # DEBUG
print(CorrAns) # DEBUG

It’s registering the correct mouse clicks and CorrAns (see screenshot below), but is still consistently giving the lose feedback and recording No in the correct column, even when the response is correct:

Any suggestions would be appreciated. Thank you

Thank you, that debugging code was useful and can now be deleted.

The issue is that mouse.clicked_name returns a list, even when it contains just a single value. So the comparison mouse.clicked_name=='image1' can never evaluate to True, because you are comparing a list containing a value to the value itself, in isolation. e.g. in the real world, an apple in a bucket is not the same as an apple by itself.

So you need to either make each side of the comparison a list, or each side a single string. e.g. Try modifying your code like this:

# compare a list to a list:
if mouse.clicked_name == ['image1'] and CorrAns == 'Image1':

or this:

# compare a string to a string: 
if mouse.clicked_name[0] == 'image1' and CorrAns == 'Image1':

Getting types wrong is one of the commonest problems in Python programming, it gets me all the time. The type() and print() functions are your best friends in diagnosing situations like this, where your code logic is correct but you still don’t get the right result.

Hi Michael, I’ve now modified the code to the first option above and it works correctly. Thank you very much!