List indices must be integers or slices, not float: error only second time referring to parameters, not first

PsychoPy v2021.2.3
Windows 10

At the beginning of an experiment parameters to be used during two separate later routines are set in the begin experiment tab of a coder block as follows:

contr_set = [1 - float(expInfo['Contrast Level']), 1]
ori_set = [5, -5, float(expInfo['Orientation Level']), -float(expInfo['Orientation Level'])] 

As you can see, this references two values entered in the popup dialogue box at the start of each experiment. Later on, values from the two lists above are selected for use as opacity values or orientation values (respectively, for a grating stimuli) using a .xlsx file, as to randomize which value is chosen each trial. Specifically, to do this ori_set[oriLeft] for example is put as the orientation property, where oriLeft is a number 0 through 3 which will select a value from the list.

The first routine this is done in works without any issues. Then, later in the experiment, a separate, but identically constructed routine, alone in its own loop throws the error message:

TypeError: list indices must be integers or slices, not float
##### Experiment ended. #####

Why would I only be getting this error for the second but not first routine these parameters are referenced? I have rebuilt the routine several times, checked that everything is spelled correctly, and that presets are correct. Also, it is important to know that two separate .xlsx files are being used for the first and second routines but that they are nearly identical, and I have checked that they are being referenced correctly.

I do have several other code components between the first and second routine, is it possible one of them is interfering? --I don’t see why any should, only one other uses a list, and it is a completely different variable.

Any advice is much appreciated.

How are you setting contrLeft2 ?

It is being set from a spreadsheet, like oriLeft is, to clarify. There are two grating stimuli displayed on the screen at once, so there is a contrRight and contrLeft used for a right and left stimuli (corresponding to opacity). The same is true for orientation. The numbers on the spreadsheet for contrLeft and contrRight are either 0 or 1 (since there are two items on the contr list). The numbers for oriLeft and oriRight are 0, 1, 2 or 3 (since there are four items on the orientation list).

In that case there might be blank rows in your spreadsheet, especially if the error doesn’t come up on the first trial.

I remade the spreadsheet a few times now, to make sure the numbers I am listing are not decimals (even by accident). I have had the rows specified in the spread sheet, so that does not seem to be the issue either. Also, it works for one routine early on in the experiment but not the same routine later, after a number of other routines. Could it be that it is adding a decimal point to the value in the builder? would I be able to solve this by putting int( somewhere, if so where?

I solved the problem the manual way. I reformatted the orientation and opacity values to (contr_set(int[contrLeft2]) for example, and now it doesn’t mistake the spreadsheet values as decimals.

1 Like