Coding correct answer as incorrect

Hey all,

I could use some help on a headache we have not been able to figure out.

I have an experiment where a participant should click on 1 of 8 jpegs pictures, 4 of the 8 pictures are count as “correct”, but I cannot get the contingency table to evaluate this. It is attached TestVegasOddsTest.xlsx (29.7 KB)

Working in builder, this is my code.

for stimulus in [DscmImg1, DscmImg2, DscmImg3, DscmImg4, DscmImg5, DscmImg6, DscmImg7, DscmImg8]:

    # check if the mouse is pressed within the current one:
    if mouse.isPressedIn(stimulus):

        # Yes, so store the reaction time in the data:
        thisExp.addData('RT', t)

        # check if the stimulus' image filename matches the correct answer:
        if stimulus.image in corrAns2:
             thisExp.addData('correct', 1)
             correct_responses = correct_responses + 1
        else:
             thisExp.addData('correct', 0)

        # end the trial once done here:
        continueRoutine = False

        # stop any further checking:
        break

What did you try to make it work?:
I have tried adjusting to---- if stimulus.image in eval(corrAns2):

However, either way, the output continues to record all responses as “0” or incorrect, even when a correct answer is selected.

Any thoughts would be appreciated. Thanks!

My guess is that your corrAns2 variable is being interpreted just as a single string of text (as PsychoPy has no way of knowing that the information in that field is supposed to represent an actual Python expression).

To turn the string of characters "['DiscrimImage8', 'DiscrimImage2', 'DiscrimImage5', 'DiscrimImage6']" into the expression (i.e. the Python list) ['DiscrimImage8', 'DiscrimImage2', 'DiscrimImage5', 'DiscrimImage6'], use the eval() function:

if stimulus.image in eval(corrAns2):
    # etc

When in doubt in such situations, sprinkle your code with (temporary) debugging code to check what values are in variables and what type they are, e.g.

print(corrAns2) # does it have quotes around it?
print(type(corrAns2)) # is it a string or a list?

Hey Michael,

Thanks for the quick response. I have updated the code accordingly…
if stimulus.image in eval(corrAns2):

However, now I receive this error…

if stimulus.image in eval(corrAns2):

TypeError: eval() arg 1 must be a string, bytes or code object

Please do this:

print(corrAns2)
print(type(corrAns2))

and tell us the answers.

Running the debugging code returns this.

print(corrAns2)
NameError: name ‘corrAns2’ is not defined

print(type(corrAns2))
NameError: name ‘corrAns2’ is not defined

Is it not recognizing the variable in the contingency file?

Yes. Either this code is running outside of the scope of the loop that is connected to the conditions file (e.g. it could be running at the start of the experiment, before the loop has been created, or within another loop), or the conditions file has not been successfully linked to the loop, or there is a spelling mistake (especially remember that variable names are case sensitive, and they have to agree exactly with what is in the conditions file).

Ok, so when I run the print(type(corrAns2)) code in the ‘Begin Experiment’ tab in Codebuilder, I get the NameError.

However, when I run the print(type(corrAns2)) code in ‘Begin Routine’, the variable returns as a ‘list’. So it recognizes the variable in the ‘Routine’. It is still returning correct response as correct. Despite using eval to convert the list to string…

It is still returning correct responses as incorrect.

Sorry, this seems to be an easy fix, I just cannot get it to work.

Please do this:

print(corrAns2)
print(type(corrAns2))

and tell us both the answers.

Here is what it returns…

[‘DiscrimImage8’, ‘DiscrimImage2’, ‘DiscrimImage5’, ‘DiscrimImage6’]
<class ‘list’>

2019-01-20 18:45:45.167 python[18617:3456362] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to (null)
Traceback (most recent call last):
if stimulus.image in eval(corrAns2):
TypeError: eval() arg 1 must be a string, bytes or code object

OK, this looks correct, inasmuch as corrAns2 is correctly being treated as a list.

So I think the reason the comparison is failing is that stimulus.image is a filename (e.g. 'Hearts Queen.jpg') whereas the contents of the corrAns2 list are string versions of the names of your stimulus components (e.g. 'DiscrimImage8', etc). These things will never be equal. Is it possible to change your corrAns list to instead contain the correct file names?

i.e. if stimulus.image in eval(corrAns2): should work, as long as corrAns2 contains a list of filenames rather than component names.

Or alternatively, you would somehow need to cycle through the list of names in corrAns, eval() them to the object they correspond to, and check if the current stimulus is one of them.

Awesome. I was just able to get it to work!

I changed the corrAns2 list to the filenames. At first it was still not working using

However, I changed the code back to

    if stimulus.image in corrAns2: 

and that seemed to do the trick.

Thank you so much for your help!

Hi, Nitta.
Thanks for your helpful post.

I meet a similar issue. May I please to know what your “stimulus” and “stimulus.image” refer to respectively?

Thanks!