简体   繁体   English

如何仅在 Python 中到达循环结束后打印失败语句?

[英]How to print a fail statement only after reaching end of loop in Python?

I have a small code snippet and need help implementing a fail statement ( No match ).我有一个小代码片段,需要帮助实现一个失败语句( 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.我遍历名为readercsv文件中的每一row ,并将该行中的每个元素从字符串转换为 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.如果两个列表匹配,我打印row[0] ,它只包含匹配列表所属的人的姓名。 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?但是,如果这两个if语句在通过for row in reader:后都失败了,我将如何 go 关于仅打印一次像No match这样的语句? Because if I write it inside the loop, this statement would be printed row number of times rather than just once.因为如果我把它写在循环中,这条语句将被打印row数而不是一次。

EDIT : Here is my (unsuccessful) implementation using bschlueter's idea.编辑:这是我使用 bschlueter 的想法的(不成功的)实现。 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")

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)

Just add one more if statement with and for catching and printing no match.只需再添加一个带有 and 的 if 语句来捕获和打印不匹配。 add this at the end of second IF statement在第二个 IF 语句的末尾添加这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM