Variable not defined pavlovia

I couldn’t find a solution using the tutorial, so I copied the code from psychopy builder which caused the problem:

------Prepare to start Routine “trp”-------

continueRoutine = True

update component parameters for each repeat

import random
randDuration = random.randint(1000, 2000)/1000

keep track of which components have finished

trpComponents = [polygon_39]
for thisComponent in trpComponents:
thisComponent.tStart = None
thisComponent.tStop = None
thisComponent.tStartRefresh = None
thisComponent.tStopRefresh = None
if hasattr(thisComponent, ‘status’):
thisComponent.status = NOT_STARTED

reset timers

t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock=“now”)
trpClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1

-------Run Routine “trp”-------

while continueRoutine:
# get current time
t = trpClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=trpClock)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame

# *polygon_39* updates
if polygon_39.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
    # keep track of start time/frame for later
    polygon_39.frameNStart = frameN  # exact frame index
    polygon_39.tStart = t  # local t and not account for scr refresh
    polygon_39.tStartRefresh = tThisFlipGlobal  # on global time
    win.timeOnFlip(polygon_39, 'tStartRefresh')  # time at next scr refresh
    polygon_39.setAutoDraw(True)
if polygon_39.status == STARTED:
    # is it time to stop? (based on global clock, using actual start)
    if tThisFlipGlobal > polygon_39.tStartRefresh + randDuration-frameTolerance:
        # keep track of stop time/frame for later
        polygon_39.tStop = t  # not accounting for scr refresh
        polygon_39.frameNStop = frameN  # exact frame index
        win.timeOnFlip(polygon_39, 'tStopRefresh')  # time at next scr refresh
        polygon_39.setAutoDraw(False)

# check for quit (typically the Esc key)
if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
    core.quit()

# check if all components have finished
if not continueRoutine:  # a component has requested a forced-end of Routine
    break
continueRoutine = False  # will revert to True if at least one component still running
for thisComponent in trpComponents:
    if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
        continueRoutine = True
        break  # at least one component has not yet finished

# refresh the screen
if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
    win.flip()

-------Ending Routine “trp”-------

for thisComponent in trpComponents:
if hasattr(thisComponent, “setAutoDraw”):
thisComponent.setAutoDraw(False)

the Routine “trp” was not non-slip safe, so reset the non-slip timer

routineTimer.reset()

set up handler to look after randomisation of conditions etc

trials_6 = data.TrialHandler(nReps=1, method=‘random’,
extraInfo=expInfo, originPath=-1,
trialList=data.importConditions(‘5_kv_1.xlsx’, selection=‘3’),
seed=None, name=‘trials_6’)
thisExp.addLoop(trials_6) # add the loop to the experiment
thisTrial_6 = trials_6.trialList[0] # so we can initialise stimuli with some values

abbreviate parameter names if possible (e.g. rgb = thisTrial_6.rgb)

if thisTrial_6 != None:
for paramName in thisTrial_6:
exec(’{} = thisTrial_6[paramName]’.format(paramName))

for thisTrial_6 in trials_6:
currentLoop = trials_6
# abbreviate parameter names if possible (e.g. rgb = thisTrial_6.rgb)
if thisTrial_6 != None:
for paramName in thisTrial_6:
exec(’{} = thisTrial_6[paramName]’.format(paramName))

The routine consists of a fixation cross in the middle and a code component for assigning the duration of the routine. It’s the only routine where I use randDuration variable. As you can see the variable is assigned in the beginning and then used later, so I don’t really know what could be the problem :frowning: