| Reference | Downloads | Github

Referring to a loop in a text component at the beginning of the code

Hello everybody,

I’d like to insert the count of remaining trials in a text component of my loop. I tried to the following:

# Initialize components for Routine "Break_warmup"
Break_warmupClock = core.Clock()
break_wp_text = visual.TextStim(win=win, name='break_wp_text',
    text=u'Now you can take the break. \n Number of trials remaining:'+str(loop_warmup.nRemaining)+'/'+str(loop_warmup.nTotal),
    pos=(0, 0), height=0.1, wrapWidth=1.5, ori=0, 
    color=u'white', colorSpace='rgb', opacity=1,

However, I got the error:

NameError: name ‘loop_warmup’ is not defined

I do understand I can’t refer to loop_warmup before defining it, but I was wondering if there is any way I could do this by keeping the text component where it is, especially because it also contain extra text.

Hope it is all clear. Thanks!

You don’t say if this is your own custom code or if it is Builder generated. If the latter, you probably just need to select that the text field updates on every routine rather than being constant.

Sorry, I forgot about that. So, I started with the Builder and then I turn to the Coder.

If there anyway I can do this in the Coder?

When you initially create the stimulus, just provide it with some dummy placeholder text, e.g. text = 'blah blah blah.

Inside the loop, you would then simply update the text component once per iteration e.g.

break_wp_text.text = u'Now you can take the break. \n Number of trials remaining: ' + 
    str(loop_warmup.nRemaining) + '/' + str(loop_warmup.nTotal)

1 Like

That seems to work, thanks! Just to make sure - I put the actual text here:

# ------Prepare to start Routine "Break"-------
    t = 0
    BreakClock.reset()  # clock
    frameN = -1
    continueRoutine = True
    # update component parameters for each repeat
    break_resp = event.BuilderKeyResponse()
    if loop.thisTrialN  not in [ int(loop.nTotal / 3) ]:
        continueRoutine=False #take a break at the 200th and 400th trials
    # keep track of which components have finished
    BreakComponents = [break_text, break_resp]
    break_text.text = "You can now take a short break.\n\n There are" + str(loop.nRemaining + 1) + "words left. \n\n When you are ready, press 'SPACE' to resume the experiment."
    for thisComponent in BreakComponents:
        if hasattr(thisComponent, 'status'):
            thisComponent.status = NOT_STARTED
    # -------Start Routine "Break"-------
    while continueRoutine:

Is that the place where it should be?

I can’t see where the TrialHandler for the loop is initialised, but I guess it is before the snippet you posted, so should be good. You’ll be able to tell when it runs easily enough.

Yes, it’s right above it. Thanks!