List length check not working in Sudoku checker
<p>I want to write some Python code to check that whether a matrix accords with the rule of Sudoku. My code is presented below:</p>

<pre><code>correct = [[1,2,3],
           [2,3,1],
           [3,1,2]]

incorrect = [[1,2,3,4],
             [2,3,1,3],
             [3,1,2,3],
             [4,4,4,4]]

incorrect2 = [[1,2,3,4],
             [2,3,1,4],
             [4,1,2,3],
             [3,4,1,2]]

incorrect3 = [[1,2,3,4,5],
              [2,3,1,5,6],
              [4,5,2,1,3],
              [3,4,5,2,1],
              [5,6,4,3,2]]

incorrect4 = [['a','b','c'],
              ['b','c','a'],
              ['c','a','b']]

incorrect5 = [ [1, 1.5],
               [1.5, 1]]

def check_sudoku(matrix):
    n = len(matrix)
    # check each row
    i, j = 0, 0
    while i &lt; n:
        checked = []
        while j &lt; n:
            if matrix[i][j] not in checked:
                checked.append(matrix[i][j])
            j += 1
        if len(checked) &lt; n:
            return False
        i += 1
    # check each column
    i, j = 0, 0
    while i &lt; n:
        checked = []
        while j &lt; n:
            if matrix[j][i] not in checked:
                len(checked)
                checked.append(matrix[j][i])
            j += 1
        if len(checked) &lt; n:
            return False
        i += 1

    return True

# the output should look like this:
print(check_sudoku(incorrect))
#&gt;&gt;&gt; False

print(check_sudoku(correct))
#&gt;&gt;&gt; True

print(check_sudoku(incorrect2))
#&gt;&gt;&gt; False

print(check_sudoku(incorrect3))
#&gt;&gt;&gt; False

print(check_sudoku(incorrect4))
#&gt;&gt;&gt; False

print(check_sudoku(incorrect5))
#&gt;&gt;&gt; False
</code></pre>

<p>But here's a weird problem in the if statement. After the two inner while loops, the statement "len(checked)" evaluates to 0 which should not be like that. I don't know what happened.</p>

<p>You don't zeroing <code>j</code> variable in inner loops (both inner loops).</p>

<pre><code>i, j = 0, 0
while i &lt; n:
    checked = []
    j = 0 # you need set j to zero in each run
          # if you don't do this: j will be equal to n in second run
    while j &lt; n:
        if matrix[i][j] not in checked:
            checked.append(matrix[i][j])
        j += 1
    if len(checked) &lt; n:
        return False
    i += 1
</code></pre>

<p>Also you need to check sub matrices.</p>

python