Finish routine when amount of trials or well performance is reached

Win10
Psychopy v2022.2.4

What are you trying to achieve?:

I am trying to finish a loop either when participants reach a good performance (5 trials are correct) or when they have already done 36 trials.

What did you try to make it work?:

I created two variables that I defined at the “Begin Experiment”

practice_points = 0
correct_decision = []
practice_trials = 0

and at “End Routine”, I tried to create a loop that checks whether they performed well enough or they reached the maximum number of trials. Every trial, practice_trials increase 1.

if practice_points >= 5 or practice_trials >= 36:
    practice_trials.finished = True  #practice_trials is the name of my loop
elif practice_points < 5 or practice_trials < 36: 
    if (mouse.getPressed()[0] == 1) and (trial == 1 or trial == 2):
        print('trial', trial)
        print('correct_press')
        practice_points  += 1
        print('practice_points',practice_points)
        correct_decision = 1
        print('corr_dec',correct_decision)
        practice_trials  += 1

What specifically went wrong when you tried that?:
I get the following error:

if practice_points >= 5 or practice_trials >= 36:
TypeError: '>=' not supported between instances of 'TrialHandler' and 'int'

I tried putting int() before the variables, eg. int(practice_trials), but I get this error:

if int(practice_points) >= 5 or int(practice_trials) >= 36:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'TrialHandler'

Could someone please help me define my variables so I can use these statements?

Hi @lucia96,

it might be a problem that you have both a variable and a loop called practice_trials. You can remove the variable and use practice_trials.thisN in your if statement instead. This refers to the number of trials that already ran in this loop. So the full code would be

if practice_points >= 5 or practice_trials.thisN == 35:
    practice_trials.finished = True  #practice_trials is the name of my loop
elif practice_points < 5 or practice_trials.thisN < 35: 
    if (mouse.getPressed()[0] == 1) and (trial == 1 or trial == 2):
        print('trial', trial)
        print('correct_press')
        practice_points  += 1
        print('practice_points',practice_points)
        correct_decision = 1
        print('corr_dec',correct_decision)

Hi @ajus ,

stupid mistake, that worked, thank you very much for your help!!

1 Like