【问题标题】:elif and functions python how to?elif和函数python如何?
【发布时间】:2020-02-05 18:31:30
【问题描述】:

我有这个代码

elif device_type == "7":
print("\n")
print("************************************")
print("*****                          *****")
print("*****   Comparision Checker    *****")
print("*****    Of Two Configs        *****")
print("************************************")
print("\n")
print('\nWARNING: Discrepancies found:')

def open_file_and_return_list(file_path):
    list = []
    with open(file_path, 'r') as f:
        line = f.readline()
        while line:
            list.append(line)
            line = f.readline()
    return list


def clean_new_line(list):
    for i in range(len(list)):
        if "\n" in list[i]:
            list[i] = list[i].replace("\n", "")
    return list


if __name__ == "__main__":
    list1 = open_file_and_return_list(r"new.txt")
    list2 = open_file_and_return_list(r"standard.txt")
    maxl = max(len(list1), len(list2))
    list1 += [''] * (maxl - len(list1))
    list2 += [''] * (maxl - len(list2))
    diff = []
    diff_file = input("\nINFO: Select what to name the difference(s) : ")
    open(diff_file, 'w').close()

    for iline, (l1, l2) in enumerate(zip(list1, list2)):
        if l1 != l2:
            print(iline, l1, l2)
            print(iline, l1, l2, file=open(diff_file, 'a'))

当我使用选项 7 时,我显然会收到此错误。

信息:选择测试:7 Traceback(最近一次调用最后一次):

文件“C:/Users/a/a/a/pre-post-check-LATEST.py”,第 866 行,在


list1 = open_file_and_return_list(r"new.txt") ***** ***** 文件“C:/Users/a/a/a/pre-post-check-LATEST.py”,第 850 行,在 open_file_and_return_list ***** 比较检查器 ***** ***** 两个配置 *****


我该如何解决这个问题,或者显然我的逻辑不正确,我该如何解决我的逻辑? 谢谢

【问题讨论】:

标签: python function file


【解决方案1】:

我没有添加整个代码

所以我能够真正解决我自己的问题!我有一堆 if 语句,我通过将 elif 放在另一个 elif 之上来打破它们。

再次感谢您让我检查我的代码,我的眼睛似乎已经呆滞了。感谢 John Gordon 和 APhillips!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    • 2017-05-03
    • 2021-04-13
    • 2022-11-23
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    相关资源
    最近更新 更多