Showing next sequence after a correct answer

OS (e.g. Win10): Win11
PsychoPy version (e.g. 1.84.x): 2024.2.4
Standard Standalone? (y/n) y
What are you trying to achieve?: In the experiment I am trying to build the participants are shown letter sequences which they have to replicate correctly in a text box. If the answer is correct, they should move on to the next sequence. If the answer is incorrect, the same sequence should be shown and replicated again.

What did you try to make it work?:
I added two loops - one to repeat the whole process for every sequence and one that should repeat single incorrectly replicated sequences.

This is the code I am using (Begin Routine):

if input.text == sequence:
    thisFeedback = 'Richtig!'
else:
    thisFeedback = 'Falsch!'

wiederholungen.addData('corrsequence', thisFeedback)

And

# Initialisiere den Zähler für die Wiederholungen der inneren Schleife
wdh_falsch_counter = 0

# Überprüfe die Eingabe
if input == sequence:
    # Wenn die Eingabe korrekt ist:
    continueRoutine = False  # Beende die aktuelle Routine und fahre mit der äußeren Schleife fort

if input != sequence:
    # Wenn die Eingabe falsch ist:
    wdh_falsch_counter += 1  # Erhöhe den Zähler für fehlerhafte Versuche
    if wdh_falsch_counter < 2:
        continueRoutine = True  # Wiederhole die innere Schleife
    else:
        continueRoutine = False  # Beende die innere Schleife und fahre mit der äußeren Schleife fort

if wdh_falsch.finished:
    continueRoutine = False

What specifically went wrong when you tried that?:
The sequences I replicated correctly were repeated (via inner loop “wdh_falsch”) although a correct answer should lead to showing the next sequence. I have no coding skills or knowledge in Python whatsoever so I am hoping any of you can see possible mistakes/solutions.
Thanks a lot!

You say that this code is in Begin Routine. If the input editable textbox is in that routine then this code needs to be in End Routine.

I think you are also misunderstanding continueRoutine.

continueRoutine = False is usually used in Each Frame code to signal the end of a routine if a certain condition is met and in Begin Routine to skip a routine that hasn’t yet started (though this latter use should now be replaced by ski if in Routine Settings.

loopName.finished = True is generallly used in End Routine to end a loop early.

So you might want something like

Begin Experiment

wdh_falsch_counter = 0

End Routine

if input.text == sequence:
    thisFeedback = 'Richtig!'
else:
    thisFeedback = 'Falsch!'
    wdh_falsch_counter += 1
    if wdh_falsch_counter == 2:
        wdh_falsch.finished = True
        wdh_falsch_counter = 0

thisExp.addData('corrsequence', thisFeedback)```

Hi, first of all thank you for helping me!
The code I posted isn´t in the same routine as the textbox, it´s in my “feedback” routine. When I added it to the “input_sequence” routine (where the editable textbox is) the way you suggested, I got following error message: “feedback_text.setText(thisFeedback)
UnboundLocalError: local variable ‘thisFeedback’ referenced before assignment”. Before, I disabled my feedback-routine and placed a text component (with $thisFeedback) in the “input_sequence” routine.

Then I put the code back into the feedback-routine (in Begin Routine) which caused the same error as in the beginning: I correctly replicated a sequence in the editable textbox but this sequence was repeated two times afterwards (although a correct input should lead to a new sequence).

Did you have the text of a text component set to $thisFeedback and constant instead of set every repeat?

It´s set to “$thisFeedback” and I tried both constant and set every repeat, both gave the same error message

Do you still have this in a code component? Setting in the text component is easier.

You could put thisFeedback = '' in Begin Experiment but that should only be needed if you are running the experiment online.

I only have the code component you wrote, so I don´t know where this “feedback_text.setText(thisFeedback)” is coming from.

Could the problem lie in the time settings of my thisFeedback-text component? I have it set to a duration of 2 seconds with no starting time.

Starting time should be 0 seconds

Oh alright, thanks! But the error message still occurs when I keep the feedback components (code and text) in the input_sequence routine.

Please could you copy and paste the full error message?

As far as I know, this is the full error message “text=thisFeedback,
UnboundLocalError: local variable ‘thisFeedback’ referenced before assignment. ioHub Server Process Completed With Code: 0
################# Experiment ended with exit code 1 [pid:5428] #################”

This error doesn´t occur when I disable the feedback components in the input_sequence routine and put them back into the feedback-routine, where I had them originally. Is there any disadvantage in having the code and text component for the feedback in an additional routine?

text=, not feedback_text.text = ? Please could you upload your psyexp file?

It’s fine to have code to sent feedback text in Begin Routine of the feedback routine if the code component is at the top of the routine.

Of course!
psychopy-test1.psyexp (41.4 KB)

Maybe (regarding my original question) you can also see if there is something wrong with the settings of my loops that could be causing the problem?

My recommendations:

Delete enter_code. It does nothing in Begin Routine and key_resp is already set to end the routine.

input is a Python function. Try changing the name of the textbox to inputText (and do the same in the code).

wdh_falsch_counter will never be 2 if you set it to 0 in Begin Routine. Move wdh_falsch_counter = 0 to Begin Experiment.

Thank you!

Unfortunately I still have the problem that the number of repititions per sequence doesn‘t respond to whether my input is correct or incorrect. Instead, each sequence is repeated once after the input. But incorrect input should lead to repeating the same sequence (up to two times) and correct input should lead to the next sequence. I tried getting directions on this by ChatGPT but that only brought up more errors🥲

Could this be caused by incorrect loop settings? Or does this process need to be specified more precisely in code?

Are you testing this online or locally?

My code should break out of the inner loop and progress to the next iteration of the outer loop after two incorrect answers. If you also want it to move on after a correct answer you could try:

if input.text == sequence:
    thisFeedback = 'Richtig!'
    wdh_falsch.finished = True
    wdh_falsch_counter = 0
else:
    thisFeedback = 'Falsch!'
    wdh_falsch_counter += 1
    if wdh_falsch_counter == 2:
        wdh_falsch.finished = True
        wdh_falsch_counter = 0

thisExp.addData('corrsequence', thisFeedback)