| Reference | Downloads | Github

Conditional loop based on randomized correct key presses

Hello! I am brand new to psychopy and trying to a build a conditional loop into my experiment, that will only enable subjects to continue to the main block of the experiment after correctly completing 3/5 of the practice trials. In the experiment, participants must press a specified key (z or m) as soon as they see a bad stimuli, and press the other specified key (z or m) as soon as they see a good stimuli. I want my code to restart the practice loop until they correctly complete 3/5 trials, which will then take them to the main block of the experiment. The problem I am facing is that the designation of the button presses (press z for good or press z for bad) is randomized using the following code component, so I can’t figure out how to indicate which key is “correct” in order to create a score to either prevent or enable participants from moving on. I’m not sure if that makes sense, but I would really appreciate any, and all help!

if keycondition>0.5:

if keycondition>0.5:

Could you please check your code? Both tests (if keycondition>0.5:) are the same, so they could be combined. Or are they actually supposed to be different tests (e.g. <= 0.5?)

But anyway, it looks like you have a variable describing what the keypress should be, given whether the stimulus is good or bad. The final piece of the puzzle is the variable defining whether the stimulus is actually good or bad. Presumably you have such a variable in your conditions file? If so, then code like this should work:

if your_key_press_variable == corpracbad and your_stim_type_variable == 'bad':
    result = 'correct'
elif your_key_press_variable != corpracbad and your_stim_type_variable == 'good':
    result = 'correct'
    result = 'incorrect'

Thank you Michael! I was able to eventually figure out how to assign the key presses similarly to how you suggested. Now I’m trying to build the conditional loop and running into even more difficulty. I want to restrict participants from moving past the practice_trials loop until they get 70% of the “csplusfear” trials correct. Here’s a visual of what my practice trials/loop looks like.

I’ve tried adding the following code under my csplus_fear_prac start routine

if csplusfearloop_prac.thisN == 0:
        number_correct = 0

and this code under the ending routine, however my practice_trials loop keeps looping continuously. The initial order of the trials is set from an xls spreadsheet which the practice_trials loop references. I know this is probably a simple task, but I can’t seem to figure out how to make it advance to the next routine based on accuracy, and really appreciate the help!

       if key_resp_csplusfear_prac.corr:
            number_correct = number_correct + 1
         if csplusfearloop_prac.thisN == csplusfearloop_prac.nTotal:
            if number_correct/(csplusfearloop_prac.nTotal + 1) >= 0.70:
                 practice_trials.finished = True

It is often worth putting in (temporary) debugging code, like:

print(number_correct/(csplusfearloop_prac.nTotal + 1))

Does .nTotal mean what you think it means? Those attributes are listed here:

i.e. I don’t think .thisN (zero-based) could ever equal .nTotal (one-based I think). But even if it did, your code would be asking “if this is the last trial of this loop, then consider ending the loop”.

i.e. perhaps you need to express in prose what you want to do, and then we can suggest what code could implement that.

Also, please do this to format your code properly (it helps us to see the indenting, and is what I did by editing your initial post)):

Thanks Michael. I have edited my previous code in my post accordingly. Perhaps that is not the best way for me to accomplish my aims.

I have 3 practice trial types (csplusfear, csplus and csminus) and 7 of these trials total within my practice loop, which appear in an order delineated by a xls file in my trialList. Every time a participant presses a button during each trial, they receive a .corr score for that trial type. Ideally I would like, after 1 full completed practice loop, my code to check whether the cumulative accuracy across all .corr scores is above 70%. If it is not, I would like my practice loop to repeat, until they reach 70% accuracy on a practice loop, after which I would like my practice loop to end, to move on to the real task.

OK. So how many repetitions are there of your four inner loops, and how are they controlled? i.e. there are four loops, and there are supposed to be 7 trials, so I guess each loop runs at least once, and possibly up to 2-3 times? The key one to specify is the loop controlling the last routine (fixloop_prac), as the accuracy-checking code needs to run on that routine.