Saving correct/incorrect textbox responses

OS: Mac OS Catalina 10.5.7
PsychoPy version: v2020.2.10
Standalone – unsure

I am building an experiment that in which participants view a video and then type a response that matches the video.

The videos and responses are on a loop with a Conditions .xlsx file attached. The loopType is set to Random. To collect data on whether participants’ answers were correct or incorrect, I used the following code in the Code Component:

if practice_type:
    if practice_type ==corrAns:
        corr = 1
        corr = 0
    trial_prac.addData('correct', corr)

When I go to run the experiment and type responses in the textbox, they all show up as 0 (aka not correct) even when the answers are correct.

This is what the xlsx conditions file looks like:

And this is what the data output looks like. All the answers show up as 0 even though some are correct. (Capitalization doesn’t appear to matter as I did another run that matched the capitalization in the Conditions file.)

Screen Shot 2021-01-31 at 3.40.23 PM

  • What is practice_type?
  • When does this code run (relative to the text box)?
  • What is the result of inserting:

It will matter after the current problem is solved - if subjects aren’t typing capitals, then the string comparison will always fail. To get around this, would suggest that you do something like this to force each side of the comparison to have the same case:

if practice_type.lower() == corrAns.lower():

Thank you for getting back to me. I’m still learning coding and PsychoPy, so I appreciate your help.

OK, then comparing practice_type to your corrAns variable will never evaluate to True, as they are quite different things. practice_type is a complicated object (a TextBox2 stimulus) that has lots of attributes (like a position, font style and colour, whether it is currently visible, and so on). The text it contains is just one of those attributes. An analogy would be that you can’t directly compare the identity of a person to a car, just because that car happens to contain a driver. You need to directly compare like-with-like, in this case:

if practice_type.text  == corrAns: # refer directly to the text attribute

This code should only run at the end of the routine, not on every frame. That way the comparison is only made when the response is complete, not while it is still being typed (and possibly getting edited).

That wasn’t designed to change what happened in the data but to give you debugging information in real-time: it should have revealed that you were comparing incompatible object types (a TextBox2 stimulus begin compared to a string of characters).

That is because you have an if: line with nothing following it. Also, as above, a TextBox stimulus won’t have a .lower() method, only strings do, so you need to apply it there:

if practice_type.text.lower() == corrAns.lower():
    # having something here is important

EDIT: PS there does seem to be a bug at the moment about the content of the text box not being stored correctly at the end of a routine, so for the timbering at least, you might indeed need to do the comparison on each frame, and just trust that the last one will supersede any previous ones: