Hi guys, I am relatively new to Python. I am trying to code a program in Python and part of it involves checking that each input only have the characters 1 to 9 occuring only once.

My code are as follow:

for i in range(1,3):
     num = str(raw_input("Enter row " + str(i-1) + ": "))

Output will be as follows with input added to the raw input statement:

Enter row 0: 112233445
Enter row 1: 112233445

Now I want the code to display two respective error message after both inputs because both inputs have the characters 1 to 9 occuring more than once. Something with an output like:

row 0 is invalid
row 1 is invalid

Problem is, I used a second for loop below the first to display only the error messages. The second for loop is not able to detect the input in row 0 but rather reads only row 1.
Is there a way to bring all the input recieved in the first for loop (both row 0 and row 1), and evaluate them both in a second loop, hence, producing my desired output above?

Recommended Answers

All 11 Replies

Yes you can store several lines of input in a list before starting to work on them

numlist = list()
for i in range(1,3):
     numlist.append(str(raw_input("Enter row " + str(i-1) + ": ")))
# then check the content of numlist

Note that in the case of 3 numbers, it could be easier to read them once on
a single line, separated by spaces or commas.

It is customary to give the error message right after the input, not to take bunch of lines and then tell which lines are not valid if the program is used interactively, not reading from file input.

One of handiest ways of dealing input from user is having own function, which returns only after correct input is given and then returns the correct line as result.

Are you using this as condition to see that there is not repeated numbers?

len(num)==len(set(num)) and all(c.isdigit() for c in num)

Do you realize that your loop leaves only last input in num, as you do not add lines in list?

Okay, I understand the customary to report errors right after each input, but since my program will get bigger with new stuff in it, I must report all errors at the very end.

I have with me a function created in another file that makes sure the characters 1 to 9 occur only once in that statement. I imported it into my current code as

once(x)

Is there a way to integrate a second loop so that it reads out the error messages at the end of each line?

How about separate list for tuples of broken lines and line number where it was (add one to get it 1-based)? enumerate might be usefull for the for loop. It is frustrating thoug for user if he misentered line 3 that he must continue entering other 27 to correct the previous line (especially with input as readable as string of numbers without breaks).

How about separate list for tuples of broken lines and line number where it was (add one to get it 1-based)? enumerate might be usefull for the for loop. It is frustrating thoug for user if he misentered line 3 that he must continue entering other 27 to correct the previous line (especially with input as readable as string of numbers without breaks).

If there are many numbers to enter, it's better to read them from an input file, or popup a reeditable text window.

r = 0
numlist = list()
for i in range(1,3):
     numlist.append(str(raw_input("Enter row " + str(i-1) + ": ")))

if not once(str(numlist)):
     print "Error: row " + str(i-1) + " is invalid."

I took the advice into account and the code as indicated above...

when I tried to enter 112233445 as first input and 112233445 as second input, it only pops up an error at the end indicating row 1 is invalid when in fact row 0 and row 1 is invalid...


Okay...My project is actually something someone gave me to think about.

"""It is to create a program that checks a completed sudoku set and detect errors in it."""

Users only have to input each row horizontally and any errors will be displayed after the program finishes.

ps. Am I even starting the program correctly?

You can compare the solution to code of Norveig (this guy is incredible: peter norvig), but that is realy master class code. Read this article many times to catch much valuable information. I did post one version of his code here in DaniWeb, but he did fixing of the old code himself later.

You must consider all three groups that sudoku number belongs not only rows. My some attempts before seeing the master code:
http://www.daniweb.com/forums/post1190575.html#post1190575

I only have to program a sudoku checker, not a solver.

It only needs to be able to tell if the inputs are a valid solution or not

I only have to program a sudoku checker, not a solver.

It only needs to be able to tell if the inputs are a valid solution or not

See my code in the other thread (the check loops starting from line 76), or the solver code neighbours definitions.

I saw it, seems to dificult for my skill level...

Anyways, someone told me this problem can be solved using only loops to iterate errors in columns and then by box.

Just what I did at the check loops starting from line 76 in the old post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.