Restarting list or variables when loop restarts

In my experiment, the routine that is shown depends on a randomly chosen value that is decided using code at the start of the experiment. There are two main loops in the experiment. The trials_7 loop is the most outer loop which sequentially goes through specific CSV files. The inner loop consists of 12 repetitions. Once those 12reps finish, the entire loop (trials_7)restarts and the next CSV file in the outer loop begins.

``````import random
#rand=[1,2,3] *4
#shuffle(rand) # randomise its order

Begin Routine:
current_rand = rand.pop() # extract one entry on each trial

#choose a new number (category) every time the loop begins
for i in range(len(rand)):
randnum=current_rand
if randnum==1:
a=1
b=0
c=0
elif randnum==2:
a=0
b=1
c=0
elif randnum==3:
a=0
b=0
c=1
``````

This worked great for the first CSV file that is read from the outer loop (trials_7); however when the loop restarts with the second CSV, the experiment ends with an error since the variable (current_rand) empties after the first 12 trials. The goal is that every time the outer loop (trials_7) restarts, the rand=[1,2,3]x4 will reset and thus reset the variable (currrent_rand). I have the number in the list repeating 4x to ensure that each routine is shown 4times before starting the next loop.

What I tried:

``````Begin Experiment:
trials_7 = 0

if trials_7.thisN == [0,1,2,3,4,5]:
rand=[1,2,3] *4
shuffle(rand)
``````

Error: AttributeError: ‘int’ object has no attribute ‘thisN’

Hello

I assume you want to compare whether thisN is 0 or 1 or 2 or 3 or 4 or 5. One way it to simplify your logical expression to

``````if trials_7.thisN < 6:
......
``````

You could also use
Begin Experiment

``````trialNr = [0,1,2,3,4,5]
``````

Begin routine

``````if trials_7.thisN in trialNR:
. . . .
``````

You also initialise a variable called trials_7 at the beginning of the experiment. But as far as I understand, you also have a loop called trials_7 This does not work. Use unique names. The error message tells you that PsychoPy is referring to the variable you declared at the beginning of the experiment. This variable (object) does not have the attribute thisN. A loop has the attribute .thisN.

Best wishes Jens

Thanks for your reply! For some reason, the fixed amount of numbers ( 4 of each type) works for the first loop, after that it becomes inconsistent (ex: five 3s, two 1s, five 2s). I also tried adding some code to my B1 routine (where the loop begins again) in an attempt to restart the rand list at the end of the routine but similar issue with the inconsistent drawing of the numbers.

``````End Routine
rand=[1,2,3] *4
shuffle(rand)
``````

Hello,

It would be helpful if you could tell us what you changed in your experiment based on these suggestions. Did you get rid of the error message?

Regarding your second question, the inconsistent number of repetitions. Once you have created rand, there is no need to initialise it a second time, just shuffle it a second time. It is unclear to us when you are shuffling or re-shuffling your rand list.

Best wishes Jens

I tried implementing the second option and updated my loop name from trials_7 to ChooseCSV:

``````Begin Experiment:
trialNR = [0,1,2,3,4,5]

Begin Routine:
if ChooseCSV.thisN in trialNR:
rand=[1,2,3] *4
shuffle(rand)
``````

This successfully got rid of the error message but resulted in the inconsistency of numbers pulled from the list rand =[1,2,3]*4. Previously, i had rand=[1,2,3] *4 and shuffle(rand) under the Begin Experiment Tab; however that is what lead to only the first iteration of the loop having enough numbers in the list to pull from. Thanks again!

Hello

when you shuffle rand at the begin of the routine, it will be shuffled each time the routine is called. So, you need to shuffle rand before the routine is called.

Best wishes Jens