views:

121

answers:

3

Hi everyone,

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 input all 9 rows the program should then go through each row and verify and see if it satisfies the conditions of a sudoku game. If it doesnt it will return a error message and display which row has an error. Now what i need help in is how to best check the rows without writing 9 different if statements. I need to incorporate a loop. How would i do this?

My progress in code so far is below:

from a5_import import *
import sys

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

print sep

print " Sudoku Verifier! "

print sep

row_0=int(raw_input("Enter Row 0: "))

row_1=int(raw_input("Enter Row 1: "))

row_2=int(raw_input("Enter Row 2: "))

row_3=int(raw_input("Enter Row 3: "))

row_4=int(raw_input("Enter Row 4: "))

row_5=int(raw_input("Enter Row 5: "))

row_6=int(raw_input("Enter Row 6: "))

row_7=int(raw_input("Enter Row 7: "))

row_8=int(raw_input("Enter Row 8: ")) 

if not check9(row0):
print "Error: row 0 is invalid."

if not check9(row1):
    print "Error: row 1 is invalid."

if not check9(row2):
    print "Error: row 2 is invalid."

if not check9(row3):
    print "Error: row 3 is invalid."

if not check9(row4):
    print "Error: row 4 is invalid."

if not check9(row5):
    print "Error: row 5 is invalid."

if not check9(row6):
    print "Error: row 6 is invalid."

if not check9(row7):
    print "Error: row 7 is invalid."

if not check9(row8):
    print "Error: row 8 is invalid."     



print sep

Again the requirements are i need the following three things to be accomplished:

  1. The program produces the correct output
  2. The program uses a loop correctly to check the columns of the input.
  3. The program uses a loop correctly to check the boxes of the input.

Thanks for your help with the verifier loops.

A: 

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 in range(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 rows

for row in rows:
    if not check9(row):
        print "Row %s is not valid"% rows[rows.index(row)] ##Prints the row position number

print sep

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

Joshkunz
A: 

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 in range (0, 9):
    answer = int(raw_input("Enter Row %s: " % count))
    if answer in row:
        PROBLEM?
    else:
        row.append (answer)
jgritty
+1  A: 

You can check the rows by converting them to sets

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

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

gnibbler
+1: This looks like the most natural approach to me. :)
EOL
can u elaborate on that? im trying to see where this can fit in and how it uses a loop.
justin
@Justin, since you already had a few answers about using loops, this answer is a hint how to write the check9 function.
gnibbler