How to use/define the name of a block loop at the begin experiment tab

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 2020.1.1
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:
I read all my stimuli from one xlsx file and used some code to divid them into four groups. I was trying to run each group according to the block trial number.
What did you try to make it work?:
Here’s the code I’m using at the begin experiment tab:

if trials_block.thisN == 0:
    study_order = list(range(0,20))
    random.shuffle(study_order)
    study_trial = 0
elif trials_block.thisN == 1:
    study_order = list(range(40,60))
    random.shuffle(study_order)
    study_trial = 40
elif trials_block.thisN == 2:
    study_order = list(range(80,100))
    random.shuffle(study_order)
    study_trial = 80
else:
    study_order = list(range(120,140))
    random.shuffle(study_order)
    study_trial = 120

What specifically went wrong when you tried that?:
I got this error:
NameError: name ‘trials_block’ is not defined.

I guess this is because I used it before it was defined. Then I tried to add this line at the top:
trials_block = [0,1,2,3]

but got this error:
AttributeError: ‘list’ object has no attribute ‘thisN’

Does anyone have any ideas on how to achieve that? Thank you so much in advance.

.thisN is an attribute of a loop, keeping count of its current iteration number. Loops don’t exist prior to the point they are shown on the timeline, but regardless, it just doesn’t make sense to ask “what trial number are we up to on the trials_block loop?”, unless the trials_block loop has actually started.

You need to describe what you actually want to achieve.

Hi @Michael, thanks for your reply! There is an inner loop inside the trials_block loop. I want to use the number of the trials_block to determine the range of a list which will be shuffled and used as an index to call stimulus.

What I want to achieve is to divide my whole experiment (a study phase + a test phase; here it isconti_color.psyexp (95.1 KB) ) into four subgroups ((study + test)*4). I was thinking to set four groups of stimulus begin the experiment like this,

for i in range(num_test):
    #assign a image item from the image list
    image_item.append(image[i])
    
    #determin the old/new status of items
 #group1
    if i < 20:
         old_new.append("old")
         corrAns_YN.append("1")
         study_hue.append(h[i])
    elif i < 40:
         old_new.append("new")
         corrAns_YN.append("2")
         study_hue.append("")
 #group2
    elif i < 60:
         old_new.append("old")
         corrAns_YN.append("1")
         study_hue.append(h[i])
    elif i <80:
         old_new.append("new")
         corrAns_YN.append("2")
         study_hue.append("")
 #group3
    elif i < 100:
         old_new.append("old")
         corrAns_YN.append("1")
         study_hue.append(h[i])
    elif i < 120:
         old_new.append("new")
         corrAns_YN.append("2")
         study_hue.append("")
 #group4
    elif i < 140:
         old_new.append("old")
         corrAns_YN.append("1")
         study_hue.append(h[i])
    else:
         old_new.append("new")
         corrAns_YN.append("2")
         study_hue.append("")

    #determine the point of the items
    if i % 2 != 1:
        point_value.append("10")
        corrAns_point.append("2")
    else:
        point_value.append("1")
        corrAns_point.append("1")

and then use the number of the trials_block to determine which group of stimulus to use (the code in the post above). In fact I’m not sure whether I’m thinking it right or wrong. Could you give me some suggestions? Thanks a lot!
Best, Xiaotong