Unable to stop routine

OS (e.g. Win10): MacOS
PsychoPy version (e.g. 1.84.x): v2022.1.4
Standard Standalone? (y/n) If not then what?: yes

What are you trying to achieve?:

In “q1”, subjects respond by pressing ‘f’ or ‘j’. Whether or not the subject responds in “q1”, “fix2” will be displayed. If the subject pressed ‘f’ or ‘j’, a different corresponding image would then appear in “q2”. However, if the subject did not respond in “q1”, I want to skip the “q2” routine, proceed to “fix3” and then continue to repeat the loop.

What did you try to make it work?:
So for the code component of “q2”, under the “Begin Routine” tab:

if q1_resp.keys:
    if q1_resp.keys == 'f':
        q2_img = yes_img
    elif q1_resp.keys == 'j':
        q2_img = no_img
else:
    continueRoutine = False

What specifically went wrong when you tried that?:
If no key was pressed in “q1”, “fix2” was displayed and then the experiment was quitted when there were supposed to be more loops to repeat. The error seems to appear at q2_image.setImage(q2_img).
NameError: name ‘q2_img’ is not defined.

You should have a default value for q2_img and set it in Begin Experiment.

For example I sometimes use an image that’s a small transparent square png.

Thank you @wakecarter! It works!