| Reference | Downloads | Github

Conditionally read in rows

MacOS Sierra 10.12.5 :
PsychoPy version 1.84.2:

I am trying to only read in questions from an excel file depending on the answer in another column from that file.

Specifically, I am trying to read in a question only if there is a ‘y’ in the “studyagain” column of my excel file.

I have the routine in the loop that references the conditions file. I then have text that displays $question. I have tried to add a code in that is as follows:

#begin experiment
studyagain = ' '

if studyagain = 'y':
    continueRoutine= True
if studyagain = 'n':
    continueRoutine = False

When I try this, I get a SyntaxError: invalid syntax. I’m not sure how to get the code to reference a column within my conditions file. Any suggestions?

Hi. In Python, we test for equality between two variables using ==, whereas = makes two things equal. So you need to use == in your conditional. Also, continueRoutine is automatically reset to True at the start of each routine, so you actually only one of those if statements. i.e:

if studyagain == 'n': # == tests for equality
    continueRoutine = False # = assigns a value to the variable

so now the program is running smoothly but it isn’t differentiating between questions that have a ‘y’ in the studyagain column versus those that have an ‘n’, and is still showing all questions. Here is what I have:

#begin experiment
studyagain = ' '

if studyagain == 'n':
   continueRoutine = False

Is there something that I’m doing wrong?

If your studyagain variable is coming from a conditions file, you shouldn’t need to define an initial value for it at the start of the experiment. That makes me suspect there is an issue with the variable name perhaps. Try this as a debugging step:

# begin routine
print('Check: ' + studyagain) # verify the value

if studyagain == 'n':
   continueRoutine = False

Thank you so much! That worked perfectly.

I have one more question. In another part of the experiment I only want to show them questions that have been given a rating between 1 and 5, and need them to show 33 of these questions. I put a count in to end the routine after 33 questions have been shown, but for some reason it is only showing 5 questions. Here is my code

#begin experiment
mycount = 0

#begin routine
mycount = mycount + 1
if mycount = 33:
      continueRoutine = False

I’m not sure why it isn’t showing 33 items.

Firstly, as above re assignment vs comparison, if mycount = 33: should be if mycount == 33:
That line should cause an error though, so not sure if it is actually being executed?

But even so, this code wouldn’t do what you want. It would skip trial 33 but trial 34 and beyond would run quite happily. If you want to terminate the trials, you not only need to stop the execution of this routine, but you need to terminate the loop that surrounds it, so that no subsequent routines run. i.e:

your_loop_name.finished = True

Lastly, given that you don’t get to trial 33 anyway, you must have some other restriction going on that stops the loop at 5 trials. Check your loop dialog, for example looking at the “selected rows” field, or in any other code components you might have.