for loop - Error when attempting to call an index from a list in python -
i'm trying make function print data_sets[51] huge list. when try run error:
line 460, in paste_up items in sublist[1]: indexerror: list index out of range
my function paste_up looks this:
# paste sheets onto billboard per provided data set def paste_up(data): sublist in data_sets: items in sublist[1]: if item[0] == 'sheet a': sheet_a( item[1], item[2]) elif item[0] == 'sheet b': sheet_b( item[1], item[2]) elif item[0] == 'sheet c': sheet_c( item[1], item[2]) elif item[0] == 'sheet d': sheet_d( item[1], item[2])
the list i'm trying test is:
['x', ['sheet a', 'location 1', 'upright'], ['sheet b', 'location 2', 'upright'], ['sheet c', 'location 3', 'upright'], ['sheet d', 'location 4', 'upright']],
any appreciated.
edit..... having issues function endlessly drawing images trying print. here example of function draws 200x500 rectangle @ specific location.
#draw sheets , images on them. def sheet_a(location, orientation): #code location , orientation of sheet_a. if location == 'location 1': if orientation == 'upright': goto(-300,0);setheading(0) elif orientation == 'upside down': goto(-300,0);setheading(180) elif location == 'location 2': if orientation == 'upright':goto(-100,0);setheading(0) elif orientation =='upside down': goto(-100,0);setheading(180) elif location == 'location 3': if orientation == 'upright':goto(100,0);setheading(0) elif orientation == 'upside down':goto(100,0);setheading(180) elif location == 'location 4': if orientation == 'upright':goto(300,0);setheading(0) elif orientation == 'upside down':goto(300,0);setheading(180) #drawing sheet_a outline , filling background. width(3);penup();begin_fill();forward(100);pendown();right(90) forward(250);right(90);forward(200);right(90);forward(500);right(90) forward(200);right(90);forward(250);color('blue');end_fill();color('black');penup()
first of change for items...
for item...
and code should like:
for sublist in data_sets: item in sublist[1:]: if item[0] == 'sheet a': sheet_a( item[1], item[2]) elif item[0] == 'sheet b': sheet_b( item[1], item[2]) elif item[0] == 'sheet c': sheet_c( item[1], item[2]) elif item[0] == 'sheet d': sheet_d( item[1], item[2])
it should work if data looks this:
data_sets = [['x', ['sheet a', 'location 1', 'upright'], ['sheet b', 'location 2', 'upright'], ['sheet c', 'location 3', 'upright'], ['sheet d', 'location 4', 'upright']]]
Comments
Post a Comment