1

I have a small code snippet and need help implementing a fail statement (No match). Here is the snippet:

for row in reader:
    # converts each string num --> int num
    i = 1
    while i < len(row):
        row[i] = int(row[i])
        i += 1

    if STR_count_large(sequence) == row[1:]:
        print(row[0])

    if STR_count_small(sequence) == row[1:]:
        print(row[0])

I iterate through each row in a csv file called reader, and convert each element in that row from a string to an int. After that, I compare the contents of the list of that particular row (from the 1st element to the end) against two functions that each contain a list. If the two lists match, I print row[0], which simply contains the name of the person who the matching list belongs to. However, if both of these if statements fail after going through the for row in reader: loop, how would I go about printing a statement like No match only once? Because if I write it inside the loop, this statement would be printed row number of times rather than just once.

EDIT: Here is my (unsuccessful) implementation using bschlueter's idea. Any help would be greatly appreciated:

exceptions = list()
            for row in reader:
                # converts each string num --> int num
                i = 1
                while i < len(row):
                    row[i] = int(row[i])
                    i += 1
                try:
                    if STR_count_large(sequence) == row[1:]:
                        print(row[0])
                    if STR_count_small(sequence) == row[1:]:
                        print(row[0])
                except (STR_count_large(sequence) != row[1:] and STR_count_small(sequence) != row[1:]) as exception:
                    exceptions.append(exception)
            if exceptions:
                print("No match")
qwert9988
  • 118
  • 13

2 Answers2

1

You could accumulate errors, then check the accumulation after the completion of the loop:

exceptions = list()
for row in reader:
    try:
        do_a_thing(row)
    except MyException as exception:
        exceptions.append(exception)
# Do something if any exceptions were added to the list
if exceptions:
    handle_exceptions(exceptions)
bschlueter
  • 3,817
  • 1
  • 30
  • 48
  • What would be the proper way to implement this? I tried using this structure, but I do not have much familiarity with `try` and `except` in python. I've added an edit to my original post that shows my version of trying to implement this based on your suggestion. – qwert9988 Jun 18 '20 at 05:32
  • 1
    The function `do_a_thing` needs to throw `MyException`. Exception handling in python is fairly simple, just go search for the docs. It looks like you'd be better off just using an if statement though, with a boolean value rather than the accumulator list. – bschlueter Jun 21 '20 at 23:02
0

Just add one more if statement with and for catching and printing no match. add this at the end of second IF statement

if row == reader[len(reader)]:#Check for last iteration
    if STR_count_large(sequence) != row[1:]: and STR_count_small(sequence) != row[1:]:
        print("No Match")
Vignesh
  • 1,553
  • 1
  • 10
  • 25
  • The problem with this is that it will print the `No match` statement `row` number of times (which in this case would be three times). I only want to print the `No match` statement once. – qwert9988 Jun 18 '20 at 05:38
  • i have edited code to check for last iteraiton based on length of reader – Vignesh Jun 18 '20 at 05:48
  • Sorry, this also does not work because you cannot call `len()` on `reader` because it is a csv. Thank you for trying though – qwert9988 Jun 18 '20 at 06:14
  • https://stackoverflow.com/a/2429260/10849457 This will help you – Vignesh Jun 18 '20 at 06:21