[Tutor] Tutor Digest, Vol 102, Issue 48
Alan Gauld
alan.gauld at btinternet.com
Tue Aug 21 09:52:25 CEST 2012
On 21/08/12 06:12, Osemeka Osuagwu wrote:
> right_product(row, col) for (row = 0, col = 18) I had anticipated this
> and added the "if col>len(data[row])-3:" statement
But because of zero indexing you need this to be either:
if col >= len(data[row])-3:
or
if col > len(data[row])-4:
ie. if row length is 10 the indexes go from 0-9 so the
highest index you can use is 6.
However you have similar problems in other places, such
as your main function. Consider:
for row in range(len(data)):
for col in range(len(data[row])):
if right_product(row, col) > prod:
source = [data[row][i] for i in range(col, col+4)]
what happens when col gets bigger than len(row)-4?
if down_right_product(row, col) > prod:
source = [data[row+each][col+each] for each in range(4)]
And this is even worse because both additions could blow up.
It's probably better if you can fix it in the for loops
rather than putting if guards in.
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list