[Tutor] Error in apparently correct code

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Aug 20 23:27:44 CEST 2012


On 20/08/2012 22:06, Rob Day wrote:
> 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'.
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Give a man a fish, and you feed him for a day; show him how to catch 
fish, and you feed him for a lifetime.

-- 
Cheers.

Mark Lawrence.



More information about the Tutor mailing list