| Reference | Downloads | Github

Random Position of Stimuli (two different images)

Dear Forum,

I’ve got a problem with coding a random Position for the Stimuli. I coded it the way it is recommended on the Psychopy help page but still I always get a ‘syntax error’. This is what I coded:

if random()>0.5:   
  targetpos = [-0.5, 0]#on the left 
  targetpos = [0.5, 0]#on the right 
if targetpos = [-0.5, 0]  
   targetpos2 = [0.5,0]
  targetpos2 = [-0.5,0] 

I put it in the ‘Begin routine’ section just as recommended by Psychopy.
And that is the output I get:

if targetpos = [-0.5, 0]
SyntaxError: invalid syntax

Any help would be very much appreciated!


In Python, we test for equality with == rather than =, so make that change just in the line that generates the error.

Although I’m still not sure that it is wise to test like that, as it is difficult to compare floating point numbers in any language. You should probably do something like this:

if random() > 0.5:   
    targetpos = [-0.5, 0] # on the left 
    targetpos2 = [0.5, 0]
    targetpos = [0.5, 0] # on the right   
    targetpos2 = [-0.5, 0] 

Thanks a lot for your help! Now the error warning disappeared but still I cannot run the whole experiment it chrashes right before it perform the coded part. Do you have any idea what Ii could try for a change? Any help would be very much appreciated.

Thanks a lot for your help I wanted to reply to you with the message below