Conditional stimuli to appear after keyboard response

Hello,
I am using Psychopy v1.85.2. I am creating a simple experiment where the participant makes a choice between two objects on the screen. They can press ‘1’ for the object on the left, or ‘2’ for the object on the right. After they have made their response, I want both objects to stay on screen, and for the chosen object to be circled. If no response is made within 4 seconds, I don’t want either object to be circled, and there will be another screen that says “No Response.” I have a polygon stimuli for the circle, and in the ‘Position [x,y] $’ input I have the variable ‘position’. I am trying to use this code block to implement this :

position: [];

if response.keys= '1' : 
      position= 0.5, 0
elif response.keys= '2' :
      position= -0.5, 0
else: 
      position= 10, 10

I have tried putting the code block in Begin Routine, Each Frame, and End Routine, but when I try to run the experiment, I get this error message:

NameError: name 'position' is not defined

Any help would be greatly appreciated!

-Joanne

Hi Joanne, there are a lot of basic Python errors in the code above. It might be a good idea to do some general Python tutorials online before getting stuck into PsychoPy code itself, so that you can spot these errors.

position: [];

I’m guessing this is the source of the error and should be:

position = [] # guessing you want to define an empty list

Semicolons aren’t necessary in Python and the colon here won’t do anything except confuse Python. But as you are going to assign a value to this variable later, you actually don’t need to pre-define it here as being an empty list (i.e. that line of code is unnecessary).

if response.keys= '1' :

First, tests for equality in Python use == rather than = (which is for assignment). i.e. here you are actually trying to make response.keys equal to '1' and check if the result of that operation was True, rather than testing if response.keys is equal to '1'.

Even then, .keys here will be a list, not a single value, so you really should test like this:

  if response.keys == ['1']:

or even better:

if '1' in response.keys:

Lastly, in this line:

position= 0.5, 0

you should explicitly wrap the values in square brackets to get a list object. If you don’t do that, the object will be a tuple instead, which might cause you problems later. Using a single = sign is correct here, as you are assigning a value rather than comparing it:

position = [0.5, 0]

Thank you so much for your reply Michael! I am very new to Python and Psychopy, so I greatly appreciate your help. I applied your fixes, and the program runs now, but no circle appears after any keyboard response. I also get this message after I exit the experiment:

UnicodeWarning: Unicode equal comparison failed to convert 
both arguments to Unicode - interpreting them as being unequal

if text == self text 

Thank you again,
-Joanne

if text == self text

What is the thing you want to compare to text? Python is getting confused here because you actually have two variables on the right hand side, rather than one. self is a special term in Python, and text is the same as the variable on the left hand side.

The line should look like this:

# compare to some other variable that contains text:
if text == some_other_variable

or this:

# compare to the text property belonging to some other object
if text == some_other_object.text

but we can’t help help suggest what some_other_variable or some_other_object.text should be without more context.

Hi Michael,

I am not trying to compare anything to text. I didn’t write this in my code, and I haven’t gotten this message before. This is the full error message showing where the program is pulling the error from, but this file isn’t my experiment:

C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\visual\text.py:275: UnicodeWarning:
Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
if text == self.text:

I don’t know if this error is independent of the problem with the circle or not, but thanks again for any advice.

This sort of confusion is why we need to see the full error message, exactly as produced. :wink:

You’re right, the error is occurring within the PsychoPy library itself rather than in your own code. But that is just the last step in the chain: Python got to that line in text.py because of something that was initiated in your own experiment code. Are there other lines in the error message that show the chain of commands leading back to the original line in your own code?