Skip to content Skip to sidebar Skip to footer

Help With A Python Sudoku Verifier With Loops

Im working on a a sudoku program for python and i need some help. The program will ask input from the user for 9 rows of numbers that hopefully contain the digits 1-9. Once they in

Solution 1:

You can check the rows by converting them to sets

ifset(row) == set(range(1,10)):
    # ok
    ...

you'll need to convert the row to a str first though

Solution 2:

Ok, I can see room for two loops in this scenario.

LoopA a loop that gets the input and LoopB a loop that checks the output an example here:

from a5_import import *
import sys

sep = "-.-.-.-.-.-.-.-.-.-.-.-.-.-.-."print sep

print" Sudoku Verifier! "print sep

rows = []

for rowNum inrange(1, 9):
    rowInput = int(raw_input("Enter Row %s: "% rowNum)) ## This is the same as int(raw_input("Enter Row +rowNum+": "))
    rows.append(rowInput) ##add the input to the list of rowsfor row in rows:
    ifnot check9(row):
        print"Row %s is not valid"% rows[rows.index(row)] ##Prints the row position numberprint sep

The use of a list of rows would be the best bet for validation.

Solution 3:

I would recommend looking at using arrays instead of doing row_0, row_1, row_2 etc.

Try something more like this:

row= []

for count inrange (0, 9):
    answer =int(raw_input("Enter Row %s: " % count))
    if answer inrow:
        PROBLEM?
    else:
        row.append (answer)

Post a Comment for "Help With A Python Sudoku Verifier With Loops"