python-2.7open-sesame

Counterbalancing in open sesame


I a writing an inline_script in open sesame (python). Can anyone tell me what's wrong here? (i think its something very simple, but i can not find it)

when i put the number in List = [1,2,3,4,5,6,7] the first line works, but the second does not work :(

BalanceList1 = range(1:7) + range(13:19) #does not work 
if self.get('subject_nr') == "BalanceList1": 

#here follows a list of commands

BalanceList2 = list(range(7:13))+list(range(19:25)) #does not work either
elif self.get('subject_nr') == "BalanceList2":

#other commands

Solution

  • In python 2.x you can do the following:

    BalanceList1 = range(1,6) + range(13,19)
    

    which will generate 2 lists and add them together in BalanceList1:

    [1, 2, 3, 4, 5, 13, 14, 15, 16, 17, 18]
    

    In python 3.x, range doesn't return a list anymore but an iterator (and xrange is gone), you have to explicitly convert to list:

    BalanceList1 = list(range(1,6))+list(range(13,19))
    

    A more optimal way to avoid creating too many temporary lists would be:

    BalanceList1 = list(range(1,6))
    BalanceList1.extend(range(13,19))  # avoids creating the list for 13->18
    

    more optimal than: