Hi, yeah i know this isn't place for this, but i'm not sure where else i could post it so i might as well give it a shot.

i'm writing a python code that checks if sudoku is solved correctly, and i have problems at the very start. I have this so far

def testsudoku(sudoku):

for i in range(9):
     test=[0,0,0,0,0,0,0,0,0]
     for j in range(9):
         test[sudoku[i][j]]+=1

     for k in range(9):
         if test[k] >1:
             return 'Nije dobar sudoku, greska u ', i, '-tom redu, i ', k, '-toj koloni'

return 'sudoku je dobar'                 

sudoku=[[2,9,5,3,1,6,8,4,7], [3,4,8,7,9,2,6,1,5], [6,7,1,5,4,8,9,2,3], [5,6,3,2,8,9,1,7,4], [1,8,7,6,3,4,2,5,9], [4,2,9,1,5,7,3,6,8], [7,3,6,8,2,5,4,9,1], [8,5,4,9,6,1,7,3,2], [9,1,2,4,7,3,5,8,6]]

testsudoku(sudoku)

I get an error in line test[sudoku[i][j]]+=1 saying IndexError: list index out of range. If i write the same thing with numbers and not i and j it works fine and since range(9) returns numbers from 0 to 8 it should also be ok. I'm not quite sure what the problem is but i'd appreciate help.

9 years ago

Comment has been collapsed.

Sorry I'm just a noob with Python and coding in general.

9 years ago
Permalink

Comment has been collapsed.

I'm just too noob with any existing code, including english

9 years ago
Permalink

Comment has been collapsed.

View attached image.
9 years ago
Permalink

Comment has been collapsed.

View attached image.
9 years ago
Permalink

Comment has been collapsed.

View attached image.
9 years ago
Permalink

Comment has been collapsed.

lol Ralph is great.

9 years ago
Permalink

Comment has been collapsed.

I didn't code anything in 2 years, programming languages i used then were c and pascal

9 years ago
Permalink

Comment has been collapsed.

I have no idea what is going on. I feel like adam zombie.

9 years ago
Permalink

Comment has been collapsed.

sudoku[i][j] is returning 9 and your test array is acessible with a number between 0 and 8 (9 elements). The problem is that you want to do test[9] and you can only go to test[8]

9 years ago
Permalink

Comment has been collapsed.

Thank you!

I'm a moron

9 years ago
Permalink

Comment has been collapsed.

change :
test[sudoku[i][j]]+=1
to:
test[sudoku[i][j]-1]+=1
and you're done

9 years ago
Permalink

Comment has been collapsed.

Did that as soon as i saw your post. I actually planned to do that when i was thinking about the code yesterday, but i forgot it in the meantime evidently. Thanks again, this helped me a lot.

9 years ago
Permalink

Comment has been collapsed.

Np, just ask if you need anything again :D

9 years ago
Permalink

Comment has been collapsed.

I think that you have this problem because of test[sudoku[i][j]], sudoku[i][j] returns 9 sometimes and the last index of test is 8...

9 years ago
Permalink

Comment has been collapsed.

Yup, works fine now, i'll try to polish it a bit more, but looking good

9 years ago
Permalink

Comment has been collapsed.

I think sudoku[i][j] is returning 9 when it should only return the max of 8.

9 years ago
Permalink

Comment has been collapsed.

Sign in through Steam to add a comment.