11. Practice Makes Perfect



I've checked this on other threads and its seems like the code should be right but it gives the error: "TypeError: list indices must be integers, not tuple" please help

to_21 = [x for x in range(1,22)]
print to_21

odds = to_21[::2]
print odds

middle_third = to_21[7,14]
print middle_third


Read the error message that python is giving you, that includes a line number. Then consider what it is saying and compare that to the code.

"list indices" is plural of list index, so you're using an invalid value as an index for a list.

The error message is also very googleable


Just use to_21 = range(1,22) and you would be fine.


You Can Try This
to_21 = range(1,22)
print odds
middle_third = range(8,15)


Ok i realized what happened, I used , instead of : in the last line

middle_third = to_21[7,14]


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.