Using if statements within a for loop

Hello all,

So within this code I have 1 block, with six trials in it. What I wish to do is to duplicate the trials, such that I have 12 of them. These will still be randomised in terms of order of when the trials appear, and ditto for the positions. What I would like is two conditions for each block. In the deceptive condition (6 trials), the subjects would be required to lie, and in the truthful condition the S’s would be required to tell the truth(6 trials). So, essentially what I could do is just duplicate the for loop, or just change the Nreps to 2. However, I do wish to hard code the information. That said, I am not sure how to incorporate what I think is an ‘if’ statement into the code, in order to achieve this. I would like the data output to remain the same, but of course with the results for each condition seperated.

print('Created EXPinfo')


#This section creates the trials

Matrix = [[0 for x in xrange(6)] for x in xrange(6)]


Matrix[0][0] = 1
Matrix[0][1] = 2
Matrix[0][2] = 5
Matrix[0][3] = 6
Matrix[0][4] = 7
Matrix[0][5] = 8

Matrix[1][0] = 1
Matrix[1][1] = 3
Matrix[1][2] = 9
Matrix[1][3] = 11
Matrix[1][4] = 7
Matrix[1][5] = 12

Matrix[2][0] = 1
Matrix[2][1] = 4
Matrix[2][2] = 9
Matrix[2][3] = 12
Matrix[2][4] = 6
Matrix[2][5] = 10

Matrix[3][0] = 2
Matrix[3][1] = 3
Matrix[3][2] = 8
Matrix[3][3] = 10
Matrix[3][4] = 5
Matrix[3][5] = 9

Matrix[4][0] = 2
Matrix[4][1] = 4
Matrix[4][2] = 8
Matrix[4][3] = 11
Matrix[4][4] = 7
Matrix[4][5] = 12

Matrix[5][0] = 3
Matrix[5][1] = 4
Matrix[5][2] = 5
Matrix[5][3] = 6
Matrix[5][4] = 11
Matrix[5][5] = 10

#This section Loads into trial handler
print('Created trials')
trialsList = []


for trial in range(6):
    thisTrial = {}
    order = range(6)
    random.shuffle(order)
    if 1:
        = "deceptive trial"
        else
        =truthful trial
    for i in range(6):
        
        id = Matrix[trial][order[i]]
        
        if(id ==1):
            image = 'Face.1.jpg'
        elif(id == 2):
            image = 'Face.2.jpg'
        elif(id == 3):
            image = 'Face.3.jpg'
        elif(id == 4):
            image = 'Face.4.jpg'
        elif(id == 5):
            image = 'Face.5.jpg'
        elif(id == 6):
            image = 'Face.6.jpg'
        elif(id == 7):
            image = 'Face.7.jpg'
        elif(id == 8):
            image = 'Face.8.jpg'
        elif(id == 9):
            image = 'Face.9.jpg'
        elif(id == 10):
            image = 'Face.10.jpg'
        elif(id == 11):
            image = 'Face.11.jpg'
        elif(id == 12):
            image = 'Face.12.jpg'
        else:
            image = id
        
        thisTrial[i] = image
    trialsList.append(thisTrial)
    
print('SavedTrials')

#These positions have to be altered in order to accomodate for the images
positions = [
 (-10, 10),
 (10, 10),
 (-10, -10),
 (10, -10),
 (-1, -10),
 (0, 10),
 ]


#This section creates the objects we need for the experiment

trials = data.TrialHandler(trialList=trialsList, nReps=1,
    method='random', 
    extraInfo=expInfo, name='trials')
thisExp.addLoop(trials)

#create a window
mywin = visual.Window([1920,1080], monitor="testMonitor", units="deg")
mywin.update()

img1 = visual.ImageStim(mywin, size=[5, 5])


Target = visual.ImageStim(mywin, size=[5, 5])

fixation = visual.GratingStim(mywin, tex=None, mask='raisedCos', size=20, units='pix')

responseClock = core.Clock()

message1 = visual.TextStim(mywin, text = "Are any of these people in the incident?")
message1.draw()
mywin.update()
response = event.waitKeys(keyList = ['space',], timeStamped = True)#this will wait for a button press confirmation from p's to continue


#Running the trials

for trial in trials:
    for i in range(6):
        # Prepare stimulus
        #fixation.draw()
        # Show it 
        if 1:
            ="Deceptive trial"
            elif 2:
                ="Truthful trial"
            Target.pos = positions[i]
            Target.image = trial[i]
            Target.draw()
        
    mywin.flip()
    responseClock.reset()
    resp = event.waitKeys()
    if resp == ["escape"]:
        core.quit()
    trials.addData('respDetect.keys', resp)
    trials.addData('respDetect.rt', responseClock.getTime())
    mywin.flip()
    
    thisExp.nextEntry()

I have (surprisingly!) been getting this error message for line 90- "SyntaxError: invalid syntax".
Any help would be hugely welcomed.

Nathan

Could you help us out by telling us what line 90 is? Although I’m guessing that it could be related to the couple of sets of lines that look like this:

Can you explain what you are trying to achieve there, so we can suggest a syntactically correct way?

There are a bunch of ways the code could be optimised, but let’s concentrate on the error first.

Hiya!

So line 90 (well actually 89 on my script) is this one:

    if 1:
        = "deceptive trial"
        else
        =truthful trial ```
So what I want to achieve is to label one block of 6 trials as the deceptive condition, and one block of 6 trials as the truthful condition-this is what I was trying to do on said line. Thus, I will have 12 trials within this block. In addition, I wish to incorporate this into this for loop already present. However, I am really unsure of how to do this. 

N

Notice in that code that the if clause doesn’t really test anything? i.e. if 1: always evaluates to True (because in Python, 1 == True). i.e. presumably in the this line, you need an equality test, like this:

if something == 1: # you need to decide what 'something' is

Then the next line doesn’t do anything (and is the cause of your syntax error). i.e. you need something on the left side of the equals sign so that it can be assigned to be "deceptive trial". e.g.

something_else = "deceptive trial"

I’m not really sure what to plug in to those “something” placeholders, but hopefully you have some idea?

Hi Michael,

Following your very helpful suggestions

trialsList = []
Matrixtrial= []

for trial in range(6):
    thisTrial = {}
    order = range(6)
    random.shuffle(order)
    Matrixtrial ==1
    if Matrixtrial ==1: 
        thistrial = "Deceptive trial"
    if Matrixtrial ==2:
        thistrial = "truthful trial"
        random.shuffle(Matrixtrial)
        Matrixtrial ==1
    for i in range(6):
        
        id = Matrix[trial][order[i]]
        
        if(id ==1):
            image = 'Face.1.jpg'
        elif(id == 2):
            image = 'Face.2.jpg'
        elif(id == 3):
            image = 'Face.3.jpg'
        elif(id == 4):
            image = 'Face.4.jpg'
        elif(id == 5):
            image = 'Face.5.jpg'
        elif(id == 6):
            image = 'Face.6.jpg'
        elif(id == 7):
            image = 'Face.7.jpg'
        elif(id == 8):
            image = 'Face.8.jpg'
        elif(id == 9):
            image = 'Face.9.jpg'
        elif(id == 10):
            image = 'Face.10.jpg'
        elif(id == 11):
            image = 'Face.11.jpg'
        elif(id == 12):
            image = 'Face.12.jpg'
        else:
            image = id
        
        thisTrial[i] = image
    trialsList.append(thisTrial)
    
print('SavedTrials')

#These positions have to be altered in order to accomodate for the images
positions = [
 (-10, 10),
 (10, 10),
 (-10, -10),
 (10, -10),
 (-1, -10),
 (0, 10),
 ]


#This section creates the objects we need for the experiment

trials = data.TrialHandler(trialList=trialsList, nReps=1,
    method='random', 
    extraInfo=expInfo, name='trials')
thisExp.addLoop(trials)

#create a window
mywin = visual.Window([1920,1080], monitor="testMonitor", units="deg")
mywin.update()

img1 = visual.ImageStim(mywin, size=[5, 5])


Target = visual.ImageStim(mywin, size=[5, 5])

fixation = visual.GratingStim(mywin, tex=None, mask='raisedCos', size=20, units='pix')

responseClock = core.Clock()

message1 = visual.TextStim(mywin, text = "Are any of these people in the incident?")
message1.draw()
mywin.update()
response = event.waitKeys(keyList = ['space',], timeStamped = True)#this will wait for a button press confirmation from p's to continue


#Running the trials

for trial in trials:
    Matrixtrial ==1
    for i in range(6):
        # Prepare stimulus
        #fixation.draw()
        # Show it 
            Target.pos = positions[i]
            Target.image = trial[i]
            Target.draw()
        
    mywin.flip()
    responseClock.reset()
    resp = event.waitKeys()
    if resp == ["escape"]:
        core.quit()
    trials.addData('respDetect.keys', resp)
    trials.addData('respDetect.rt', responseClock.getTime())
    mywin.flip()
    
    thisExp.nextEntry()

Unfortunatly, while I understand that much better now, I am still unsure as to how to clone the trials, such that the six stimuli appear twice- i.e to create 12 trials. I believe that the code I have now placed e.g. if Matrixtrial ==1: thistrial = "Deceptive trial" if Matrixtrial ==2: thistrial = "truthful trial" dosen’t actually have any purpose unless there is another trial created to name as the deceptive trial (if I assume that the truthful trial is already there as the 6). Any help with that would be hugely appreciated.

Nathan

You need to be careful with the distinction between = and ==:
https://discuss.codechef.com/questions/87006/what-is-the-difference-between-and-in-python

e.g. here:

Matrixtrial == 1
if Matrixtrial == 1: 
    thistrial = "Deceptive trial"
if Matrixtrial == 2:
    thistrial = "truthful trial"

The first line doesn’t really do anything, as it is just the statement “Matrixtrial is equal to 1”. I’m assuming you actually want to set the value like this: Matrixtrial = 1

But then the if statements below are a bit pointless, because Matrixtrial will always be equal to 1, so this trial will always be "Deceptive trial". It’s hard to give much advice here, as the logic isn’t self-evident.

Have you considered trying to use the Builder approach instead?