First trial is not displaying as expected, but the rest are

Hello there. I am setting up a loop on Builder, where stimuli sentences have to be presented in a specific order. The stimuli are displayed correctly in all trials except the very first one, and I cannot understand why (I have virtually no coding experience, so most of the code was generated by ChatGPT).

The idea is that: a 1) Context sentence is presented, 2) first key press, 3) Question sentence appears on screen too, 3) second key press, which hides Context and Question and triggers the presentation of a fixation cross (for 500 ms) and a fragmented answer. The code component for this presentation is the following:

Step 1: Wait for first key press to reveal the question

if not first_key_pressed:
keys = event.getKeys(keyList=[‘space’])
if ‘space’ in keys:
first_key_pressed = True
textQuestionP.setAutoDraw(True) # Display the question
win.flip() # Refresh display
print(“DEBUG: First key press detected. Question displayed, context remains visible.”)
else:
print(f"DEBUG: Waiting for first key press. Context visible: {textContextP.autoDraw}, Question hidden: {textQuestionP.autoDraw}")

Step 2: Wait for second key press to remove context and question, and display fixation cross

elif first_key_pressed and not second_key_pressed:
keys = event.getKeys(keyList=[‘space’])
if ‘space’ in keys:
second_key_pressed = True
textContextP.setAutoDraw(False) # Hide context
textQuestionP.setAutoDraw(False) # Hide question
fixationCross_P.text = ‘+’ # Set fixation cross text
fixationCross_P.setAutoDraw(True) # Display fixation cross
win.flip() # Refresh display to show fixation cross
core.wait(0.5) # Pause for 500ms
fixationCross_P.setAutoDraw(False) # Hide fixation cross
win.flip() # Refresh display to clear fixation cross
show_answer = True # Begin fragmented answer phase
word_index = 0
word_timer = current_time + 0.4 # Schedule first word display
print(“DEBUG: Second key press detected. Context and question hidden. Transitioning to fragmented answer display.”)
else:
print(f"DEBUG: Waiting for second key press. Context visible: {textContextP.autoDraw}, Question visible: {textQuestionP.autoDraw}")

Step 3: Fragmented answer word by word

elif show_answer:
answer_words = current_trial[“Answer”].split()
if word_timer is None:
word_timer = current_time + 0.4 # Initialize word timer
print(“DEBUG: word_timer initialized.”)
if word_index < len(answer_words) and current_time >= word_timer:
AnswerTextP.text = answer_words[word_index]
AnswerTextP.setAutoDraw(True)
win.flip()
print(f"DEBUG: Displaying word {word_index + 1}: {answer_words[word_index]}")
word_index += 1
word_timer = current_time + 0.4 # Schedule next word
elif word_index >= len(answer_words):
AnswerTextP.setAutoDraw(False)
win.flip()
show_answer = False
decision_phase_started = True
decision_time = current_time + 1.0
print(“DEBUG: Fragmented answer fully displayed. Preparing decision prompt.”)

According to the debug statement that I get, it should be displaying correctly, so I am a bit lost.

Here are some links.