Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 17 Online
OpenStudy (anonymous):

Hello, I have a little problem with my code, i get the error I cant determine why: >>> twoplusodds [2, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99] >>> for i in range(1,twoplusodds[-1]): print twoplusodds[i] 3 5 7 9 11 ... ... 97 99 Traceback (most recent call last): File "", line 2, in print twoplusodds[i] IndexError: list index out of range That error should not be, and I cant spot the bug.

OpenStudy (anonymous):

if I just write: >>> twoplusodds[-1] 99 as you can see, it works fine. but when its in that code - it gives the : IndexError: list index out of range

OpenStudy (anonymous):

Your range is going from 1-98. Your has only about half of that many indices. You should do 0 (indices start at 0, not 1), len(twoplusodds) instead. http://codepad.org/q4uVC5gi

OpenStudy (anonymous):

Alternatively, you don't even need a range, you could just do: http://codepad.org/9mGj65A4

OpenStudy (anonymous):

thankyou. the second choice still gave me an: for i in twoplusodds: TypeError: 'function' object is not iterable as there is more to the code, which i didnt mention. But the 1st choise, where I use "len" worked perfectly. cheers

OpenStudy (anonymous):

This is python being a jerk. You can only name one thing twoplusodds. So you must have a function named twoplusodds that's overwriting the array value.|dw:1318365972676:dw|At least this is my guess. You didn't post the code, so I can't know for sure.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!