First of all, here’s the code:
def check_sudoku(n):
d=len(n)
i=0
s=1
while i<d:
print "i=",i
while s<d:
print "s=",s
if n[0][i]==n[s][i]:
return False
s=s+1
i=i+1
return True
what I want to do is that after the value of s changes from 1 to d, then it loops again and the value of i changes.
But in my code the value of i is not changing at all.
Just to be clear of what I want to do,
say
n =[[1,2,3,4],
[2,3,1,3],
[3,1,2,3],
[4,4,4,4]]
i want the following to happen:
-
first it should check for
n[0][0]==n[1][0] n[0][0]==n[2][0] n[0][0]==n[3][0]after that value of
ishould increase by 1 -
then it should go like this:
n[0][1]==n[1][1] n[0][1]==n[2][1] n[0][1]==n[3][1]
after this value of i will increase again and this same loop will run.
This is not happening and I am not sure why.
Please tell me what changes I should make to make it run the way I want to.
Your inner loop increments
suntil it is greater thand, but you never set it back to1. Also, with your current indentation, you also incrementiin the inner loop, so it’ll reachdinside the inner loop causing the outer loop to only be run once too.So, if
dis 4, your values will go:You’d be much better off using
forloops usingrange(), avoiding your mistakes altogether:With
rangethe inner loop will always start at1, range through tolen(n)every time without explicit resets needed.If you use the
any()function you can collapse the whole test into one line: