| Reference | Downloads | Github

Conditional Image Display?

**PsychoPy version 1.85.6:

Hi all, I am designing a study within Psychopy that will utilize an amended version of the Posner Cueing Task. The goal is to have two images (one of a white man and one of a black man) displayed simultaneously on either the left or right side of the screen. Each image must randomly chosen and so must the side of the screen on which they appear.

I’m trying to insert code into the builder in order to have the images selected and placed randomly. However, I have almost no coding experience :grimacing:.

Below are screenshots of code I’ve amended from the Psychopy documentation website. (WF is white face, BF is black face)

When attempting to run this, I receive an error message that reads:
if targetPos=[-0.5, 0.0]#on the left
_ ^_
SyntaxError: invalid syntax

This is confusing to me because all I have done is amend the code from the Psychopy documentation. I would greatly appreciate any coding help or other solutions for my image display issue. I can also provide deeper explanation if this isn’t clear.


We’ve all been there. :slight_smile: It looks like all you need to make your conditional statement valid is a double equal sign (==) and a colon. Does that do the trick?

@Cesco thanks for the quick response! That was definitely a problem but I’m still getting an error message.

_if targetPos==[-0.5, 0.0]#on the left_

_ ^_
SyntaxError: invalid syntax

I have a hunch that the issue might be due to the program not recognizing targetPos and bftargetPos as the position condition for the images but I can’t seem to get it to recognize…

There is a missing colon here:

if targetPos == [-0.5, 0.0]:  # added colon here

Please note that the assignments were correct, e.g.:

    bftargetPos = [0.5, 0.0]

It might be a good idea to leave at least one space before adding comments. I find this makes the code easier to read.


Like what Jan hinted at: try learning the difference between assignments (=) and conditional statements (==).

So if I understand correctly: you pick a random float between 0.0 and 1.0 to determine the position of your targets/images. And if the target position is randomly positioned left, then the other image (bftargetPos) should be presented on the right and vice versa. Correct?

  • Instead of randomising, is there any particular reason why you don’t use a conditions file to get a full-factorial design?
  • Why are you renaming the targetPos variables in your code_2 to a string? (Oh I see now, that happens after the routine so shouldn’t be a problem. Although in that case you don’t need line two of each statement).

The following should work. Good luck!

# begin routine
if random()>0.5:
     targetPos = [-0.5, 0.0]
     targetPos = [0.5, 0.0]
# end routine
if targetPos == [-0.5, 0.0]:
     bftargetPos = 'right'
     bftargetPos = 'left'