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")