psychopy.org | Reference | Downloads | Github

PsychoPy Builder Problem in Running Nested If Codes

Windows 11
PsychoPy version: 2021.2.3

First, I should say that I’m rather new to programming, so it’s possible that my problem has an easy fix, but I have exploited every resource and was not able to solve it. Hopefully, someone can help me here.

The task that I am developing involves a loop with 32 conditions. However, I do not want all the conditions to be presented. Instead, I would like it to decide how many trials (out of those 32 conditions) are going to be occurring based on participant’s response to one question. For that to happen, I have added a question with two keyboard responses of p and q in a routine before myl loop. If the participant presses p, I want it to run 3 trials in the loop; if the participant presses q, I want it to run all the trials until the participant presses Enter to quit the loop.

Everything works fine except one nested if code; the code that I use to make the loop stop at 3 trials if the participant presses p. Here is my code at the last routine of my loop: (Auto → JS)

Begin Experiment:
myCount = 0

Begin Routine:
myCount = myCount + 1

End Routine:

if (myCount >= 3):
    if (repn.key == 'p'):
        test_trial.finished = True

When I run the code without the “if (repn.key == ‘p’)” condition, the experiment runs well and stops at 3 trials. But when I add the second if statement, the experiment suddenly stops at the end of the 3rd trial after giving a response and the experiment window closes without going to the final routine and saving the data.

I have tried to solve this problem by bringing the routine that involves p & q question to another loop that contains both the p & q routine and the actual loop, but the same problem occurs. Unfortunately, there are not any error messages related to the condition in the PsychoPy Runner window so that I could know why the experiment stops. It seems like an issue with nested if statements but I don’t know what it is exactly. I would really appreciate your help.

Hello!

I think it might be that you need to use “keys” instead of “key”

if (myCount >= 3):
    if (repn.keys == 'p'):
        test_trial.finished = True

Hi Becca!

Thank you for your response! I checked the “keys” word on my code and it was spelled correctly there, but I spelled it wrong in here. However, your comment made me think to go and check all the spelling for my code. I noticed that I have changed the name of the keyboard response variable without adjusting the name change in my code. I corrected it and now it is working! Thanks a lot for your help, your community has helped me a lot troubleshooting my experiments. As a person new to programming I wanted to say that I really appreciate what you folks are doing here, it is really helpful.

1 Like