Blocks of forms and conditional statement

Dear all,

My current experiment sequence involves rating different flavours of chocolate, and continue to particular forms to do willingness to pay (WTP), only their favourite and least favourite flavours will be selected:

Within each flavour, I provided the loop an excel sheet with all of the conditions they need to fulfil in the Psychopy Form format, such as:

I copied from some previous example: Blocks of rating forms - #10 by bubu

It worked out great, but now it’s asking me to define the variables suddenly. In the selectform code component:
Begin Experiment:

formList=['milk.form', 'dark.form','insect.form','chilli.form','liqorice.form']
flavours = [0,0,0,0,0]

Begin Routine:

# Selecting the lowest and highest ratings of chocolate flavours 
liked = formList[(sliderResponses.index(min(sliderResponses)))]
disliked = formList[(sliderResponses.index(max(sliderResponses)))]

choices = [liked, disliked]
for i in choices:
    fixedchoice = formList.index(i)
    flavours[fixedchoice] = 1

In each flavour, I insert the following code -
Begin Routine:

if flavours[0] == 0:
    continueRoutine = False 


# Reset the form and load a new question file
milk.formElements = {'itemIndex': [], 'question': [], 'response': []}
milk.items = milk.importItems(con)
milk._doLayout()

# Continue button 
continueButton = visual.ButtonStim(win, labelText= "Continue", pos=(.35, -.4))

Each frame:

# If the form is complete, end the routine and save the form data
if milk.formComplete():
    continueRoutine = False
    milkData = milk.getData()
    while milkData['questions']:
        for dataTypes in milkData.keys():
            thisExp.addData(dataTypes, milkData[dataTypes].popleft())
        thisExp.nextEntry()

However, I’m getting error message of:

    while milkData['questions']:
NameError: name 'milkData' is not defined

and if I defined milkData at the begin experiment as : milkData = [], I get the following error:

    while milkData['questions']:
TypeError: list indices must be integers or slices, not str

Where did it go wrong? How do I fix it?

Many thanks!!!

Diana

It definitely looks like the first time you index milkData['questions'] is before milkData = milk.getData(), the first error is the variable not being defined, as you correctly identified, the second is that you’ve defined it as a list, which can’t be indexed with strings. To make a placeholder which will allow you to get milkData['questions'] without an error you’ll need to make a dict with the correct key, like this:

milkData = {'questions': []}

I’m not sure how this will affect your other components, but it should allow it to continue at least, so give it a go and see how the data looks :slight_smile: