Text Component is not presenting the way I want it to (presents entire list twice before moving on to the next trial)

OS (e.g. Win10): Win11
PsychoPy version (e.g. 1.84.x): v2024.2.1post4
Standard Standalone? (y/n) If not then what?: Yes

What are you trying to achieve?:

This is a followup to my previous thread (see previous thread here).

In my experiment the participant must click on a point on a slider scale , where there is a topic on each end. Wherever they click , the topic that is closest (for some conditions) or farthest (for one condition) on the scale get recorded for use for a later part of the experiment. the slider goes from -3 to +3, and for conditions C and HI positive ratings should result in ChoiceB and negative ratings in ChoiceA, while in condition LI it should be reversed. This all happens in the Choose_Topic_trials loop (see screenshot below)

After being recorded on a temporary list, the topics are presented in a text variable in the Agency_trials loop in the same experiment. Also being presented in the Agency_trials loop is a text blurb from an excel sheet imbeded in the loop (presented in a text component named Fact_text1).

Here are pictures for clearer understanding:

Agency_trials loop (where the routine Facts_present is located -all loops in this experiment are sequential for simplicity’s sake, and will be conterbalanced on the excel sheet instead of in psychopy itself):
Screenshot 2024-09-03 231301

Facts_present routine (you can also see the Agency_trials loop, the Choose_topic routine and the Choose_Topic_trials at the bottom) :

Excel sheet embedded in Agency_trials loop:

what it looks like on pilot with the stimuli all together (topics are top center, text blurbs in the middle):

What did you try to make it work?:

To create and store the list, in the Choose_topic routine I added a code component named “Choice_code.” I used this code for the coding component that needs to be done at the begining of the experiment:

# Initialize an empty list to store ratings
AnswerStore = []

I used this code for the coding component that needs to be done each frame:


if Type == "C":
    if Choose_slider.getRating() is not None and Choose_slider.getRating() > 4:
        Answer=ChoiceB
    else:
        Answer=ChoiceA
elif Type == "HI":
    if Choose_slider.getRating() is not None and Choose_slider.getRating() > 4:
        Answer=ChoiceB
    else:
        Answer=ChoiceA
elif Type == "LI":
    if Choose_slider.getRating() is not None and Choose_slider.getRating() > 4:
        Answer=ChoiceA
    else:
        Answer=ChoiceB

And this code at the end of the routine to save the topics onto a list:

AnswerStore.append(Answer)

In order to present the list of topics (one by one or one per trial), I added a code component in the Facts_present routine called “Answer_code”. I used this code for the coding component that should be done each frame:

AnswerStore
# Loop through each item in the list
for item in AnswerStore:
    # Create a TextStim object for the current item
    text_stim = visual.TextStim(win, text=item, height=0.05,pos=(-0.1,0.4), color='white')
    
    # Draw the text and update the window
    text_stim.draw()
    win.flip()
    
    # Wait for a key press to continue to the next item
    event.waitKeys(keyList=['space'])

What specifically went wrong when you tried that?:
The topics are presented, and do present in the correct order (because everything in this experiment is presented sequentially), but for each presentation of the Fact blurb (presented in a text component named Fact_text1) the entire list of topics is presented twice before moving on to the text fact blurb in the trial. This gets repeated for all the trials in the loop.

I am not sure how to go on from here, or what I should do so that the items on the list are presented sequentially, one item per trial.

Thank you to everyone who has helped me solve my code issues so far. I will have to ask for your help again on this issue. Hopefully it will be smooth sailing after this is resolved ( unless something happens when i sync this experiment to Pavlovia…I will cross that bridge when I get there I guess)

I figured out a solution!

the code component in the Facts_present routine should be as follows:

In the Begin Experiment tab add this code:

AnswerStore # Or your variable with your list of items
trial_num = 0  # Trial counter
current_item = ''  # # Variable to hold the current item

In the Begin Routine tab add this code:

# In Begin Routine, assign the current item for the first trial
if trial_num < len(AnswerStore):
    current_item = AnswerStore[trial_num]  # Get the current item from the list
else:
    continueRoutine = False  # End the loop if no items are left

In the End Routine tab add this code:

trial_num += 1  # Move to the next item for the next trial

Most Importantly, You must add a Text component in the Facts_present routine that references the current item in the text, like so :
Screenshot 2024-09-12 170412

And this text component must be placed AFTER the code component in your routine:

I don’t know why but if it was placed anywhere else the first item on the list will not be shown.

Thank you @wakecarter and @umb who responded to my previous post and put me on the path to solve this issue, your help in much appreciated!

1 Like

I recommend putting code components at the top of the routine unless you have a good reason to put them at the bottom.

i usually just put the code above the thing it applies to but that is sound advice as well