Receiving KeyError while trying to terminate a loop

Hi,

I’m using PsychoPy v2024.2.4 and what I am trying to do is to end my practice loop after 5 trials. This bit of code;

’’if practice.thisN == 4:
practice.finished = True’’

in the “end routine” tab used to work perfectly but suddenly it started to terminate the whole experiment instead of skipping to the next routine while giving a “KeyError.”
(raise KeyError(key) from err
KeyError: 5)

I tried adding this part to the “begin routine” tab but it didn’t help.

’’if practice.thisN == 4:
continueRoutine = False’’

I also tried other solutions suggested here (e.g., skipping-a-routine-after-a-number-of-loops, psychopy-ends-experiment-when-loop-is-terminated) but couldn’t make it. Any help would be so much appreciated. Thanks!

Hi There,

I suspect this is because the key is read as a string rather than and integer. Could you try adjusting your code from:

if practice.thisN == 4:

To

if practice.thisN == '4':

Hopefully that does the trick!

Becca

.thisN is usually an integer not text. However, I feel like I have come across this issue. Are you using any other code related to the loop, such as rewind trials?

Helloo,

Thank you both! Unfortunately, quotation marks didn’t work.

The task is a dot task where two circles with different numbers of dots are presented on the left and right sides of the screen. I have the following codes to tell PsychoPy to randomly choose images of these circles from randomly chosen folders.

left_folder = ‘Dots/stim_0’ + str(leftImage)
left_files = os.listdir(left_folder)
left_file = random.choice(left_files)
left_path = left_folder + ‘/’ + left_file

right_folder = ‘Dots/stim_0’ + str(rightImage)
right_files = os.listdir(right_folder)
right_file = random.choice(right_files)
right_path = right_folder + ‘/’ + right_file

I’m also attaching my conditions file in case it is useful.

dot_trials.xlsx (10.4 KB)

I’ve checked my records and found that I got a weird Key error of the next iteration number when using trials.finished = True in one particular experiment (sentence repetition) in a bespoke PsychoPy environment but it was fine in others using the same version. I fixed it by implementing trials.skipTrials(nConditions - trials.thisN).

Please could you send me your psyexp file so I can try to replicate the issue?

1 Like

oh thank you so much!!!

it might seem a bit messy but if you help me figure out what to do with the practice loop, hopefully i will take care of the rest! do i need to send other excel files or image folders as well?

indep.psyexp (196.0 KB)

heyyy again!

I implemented your solution and it worked! I used it as the following:

if practice.thisN == 4:
nConditions = len(practice.trialList)
practice.skipTrials(nConditions - practice.thisN)

thank you so much again!

I’m surprised that this bug hasn’t come up more often. Here’s a minimal demo that still shows it.

It seems to be related to saving the data from a loop and this still gives the error in our pre-release versions so it hasn’t been fixed yet. On the other hand, it doesn’t seem to always occur whenever .finished = True is used so there must be something different about this demo experiment from others I’ve tried.

loop-test.psyexp (22.4 KB)

dot_trials.csv (701 Bytes)

Well, I guess you’re right! I just changed the data-saving settings and “.finished = True” works without a problem when “Save Excel File” isn’t checked. I suppose this is an error about the new version of PsychoPy.

Yes – @TParsons has identified the cause and has a solution, but needs to check it doesn’t cause any unintended consequences.