I want to shorten the lower half of this code into a loop statement.

The only(x) function is an imported function that makes sure 1 to 9 occurs only once.

num_1 = str(raw_input("Enter set 1: ")
num_2 = str(raw_input("Enter set 2: ")

if not only(num_1):                     ##These two "if not statements" must go in a loop##
    print "Error: set 1 is invalid"
if not only(num_2):
    print "Error: set 2 is invalid"

So I want the "if not statements" to be in a loop so that the code can be shorter and eliminate repetition.

The new loop however, should be able to print out the error messages with the correct "set number" that holds the error...

My new code as follow, I have created a list called num to try to iterate a loop. I feel like this is not going anywhere and I am right...

num_1 = str(raw_input("Enter set 1: ")
num_2 = str(raw_input("Enter set 2: ")

num = str([(num_1), (num_2)])

###I need to shorten the above code in this spot into a loop statement###

A universal trick is to avoid numbered variables like num_1, num_2, but rather have a list 'num' and access the data as num[0], num[1]. This allows you to write loops. So you would have

num = list()
for i in range(2):
    num.append(str(raw_input("Enter set %d: " % (i+1))))
errors = [i for (i, line) in enumerate(num) if not only(line)]
if errors:
  print "Error: sets %s are invalid." % str(tuple(i+1 for i in errors))

Edited 6 Years Ago by Gribouillis: n/a

What if I want to evaluate that each input has exactly five characters, if not, then error using a second loop for a second error message...Meaning how to I integrate a second for loop statement to read out the listed input and not mess up the first for loop?

Edited 6 Years Ago by Limiter: Purpose changed

What if I want to evaluate that each input has exactly five characters, if not, then error using a second loop for a second error message...Meaning how to I integrate a second for loop statement to read out the listed input and not mess up the first for loop?

Write pseudocode first to build the sequence of necessary actions. There are different ways to do it

for i, line in enumerate(num):
  if line i has first kind of error:
    print line i has the first error
  if line i has the second kind of error:
    print line i has the second kind of error

or

for i, line in enumerate(num):
  if line i has first kind of error:
    print line i has the first error
for i, line in enumerate(num)
  if line i has the second kind of error:
    print line i has the second kind of error

You could even define a list of possible errors and iterate over this list

Okay...so I understand that you don't recommend variables if loops are to be involved.

However, I have to use a bit of variables here, perhaps, to indicate doubles form 1 to 9 vertically. So if input looks like this:

123456789
222222222

from the raw_input statements

num_1 = str(raw_input("Enter set 1: ")
num_2 = str(raw_input("Enter set 2: ")

Then an error should appear for the second vertical. (Number 2 happened twice vertically there, should be only once for all rows)

So to design this code, i appended variables to indicate the vertical grid:

vertical_0 = num_1[0] + num_2[0]
vertical_1 = num_1[1] + num_2[1]#<--this is where the double 2s occured in the above eg

###...code continues to vertical_8, which is the 9th vertical column.

But like you said, variables does not work well with loops, is there a simpler bypass around this, I am a begginner here so I do feel sort of silly...

Okay...so I understand that you don't recommend variables if loops are to be involved.

However, I have to use a bit of variables here, perhaps, to indicate doubles form 1 to 9 vertically. So if input looks like this:

123456789
222222222

from the raw_input statements

num_1 = str(raw_input("Enter set 1: ")
num_2 = str(raw_input("Enter set 2: ")

Then an error should appear for the second vertical. (Number 2 happened twice vertically there, should be only once for all rows)

So to design this code, i appended variables to indicate the vertical grid:

vertical_0 = num_1[0] + num_2[0]
vertical_1 = num_1[1] + num_2[1]#<--this is where the double 2s occured in the above eg

###...code continues to vertical_8, which is the 9th vertical column.

But like you said, variables does not work well with loops, is there a simpler bypass around this, I am a begginner here so I do feel sort of silly...

As I said, you should use lists to store the user's input. You can build rows and columns like this

rows = list()
for i in range(8):
    rows.append(raw_input("Enter row number %d: " % (i+1))
cols = list()
for j in range(len(rows[0])):
    cols.append(''.join(row[j] for row in rows))

print rows
print cols

Perhaps you should check that all rows have the same length before building the columns.

Edited 6 Years Ago by Gribouillis: n/a

Superb answer...now, say for example, a sudoku puzzle have 9 boxes...the question regarding row and column creation is set forth already....how is it to use the same list spending to create "boxes" so that I can verify 1 to 9 occur only once using a loop.

You can test for membership in the rows list (already input), assuming you want to do this on the input value.

if input_num not in rows:
    rows.append(input_num)
#
# or remove each number as it is entered
input_list = [str(x) for x in range(1, 10)]
while len(input_list):
    print "Enter a number from", input_list
    num = raw_input("Enter: ")
    if num in input_list:
        input_list.remove(num)
## etc

Edited 6 Years Ago by woooee: n/a

As I said, you should use lists to store the user's input. You can build rows and columns like this

rows = list()
for i in range(8):
    rows.append(raw_input("Enter row number %d: " % (i+1))
cols = list()
for j in range(len(rows[0])):
    cols.append(''.join(row[j] for row in rows))

print rows
print cols

Perhaps you should check that all rows have the same length before building the columns.

I just noticed one thing above...The code does not indicate the name of the column...

For example...inputs

1123456789
4567896785


If I want it to read column 0 (The first column), output should be:

Column 0 is 1, 4

...or something like this...indicating the name of the column to be specific.

This question has already been answered. Start a new discussion instead.