[Tutor] Error in apparently correct code

Rob Day robert.day at merton.oxon.org
Mon Aug 20 23:06:12 CEST 2012


Your problem is in this line, as the traceback shows:

>  return reduce(lambda x,y:x*y, [data[row][i] for i in range(col, col+4)])
>
>
So the thing you have to think about is - what values can col be? The
answer is then in this line:

for col in range(len(data[row]))

And since each row of your data has 20 numbers in, col ranges between 0 and
19.

What happens when col is 19 and you try to do 'return reduce(lambda
x,y:x*y, [data[row][i] for i in range(col, col+4)])'? i will vary between
19 and 22, and so at some point you'll be trying to pick the 21st, 22nd and
23rd elements of a list of 20 numbers. Obviously you can't do that - so you
get a 'list index out of range'.

-- 
Robert K. Day
robert.day at merton.oxon.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120820/dde3f3e2/attachment.html>


More information about the Tutor mailing list