【问题标题】:How do I use raise correctly?如何正确使用 raise?
【发布时间】:2019-07-07 20:59:43
【问题描述】:

有人可以帮我在这段代码中获得某种结构吗?我是新来的。错误应捕获不存在的文件,以及不包含由“;”分隔的四部分行的文件。

程序应该如下所示:

测验文件的名称:hejsan
“导致输入/输出错误,请重试!”

测验文件的名称:namn.csv
“文件格式不正确。 需要有四个字符串,用 ; 分隔在文件的每一行中。”

测验文件的名称:quiz.csv

quiz.csv 满足所有要求!

def get_quiz_list_handle_exceptions():
    success = True
    while success:
        try:

            file = input("Name of quiz-file: ")
            file2 = open(file,'r')

            for lines in range(0,9):
                quiz_line = file2.readline()
                quiz_line.split(";")

                if len(quiz_line) != 4:
                    raise Exception

    except FileNotFoundError as error:
        print("That resulted in an input/output error, please try again!", error)

    except Exception:
        print("The file is not on the proper format. There needs to be four strings, separated by ; in each line of the file.")

    else:
    success = False


get_quiz_list_handle_exceptions()

【问题讨论】:

标签: python for-loop exception raise


【解决方案1】:

您的代码中存在缩进错误

def get_quiz_list_handle_exceptions():
success = True
while success:
    try:

        file = input("Name of quiz-file: ")
        file2 = open(file,'r')

        for lines in range(0,9):
            quiz_line = file2.readline()
            quiz_line.split(";")

            if len(quiz_line) != 4:
                raise Exception

    except FileNotFoundError as error:
        print("That resulted in an input/output error, please try again!", error)

    except Exception:
        print("The file is not on the proper format. There needs to be four strings, separated by ; in each line of the file.")
    else:
        success = False


get_quiz_list_handle_exceptions()

【讨论】:

    【解决方案2】:

    你有很多问题:

    1. 未能在多个位置正确缩进
    2. 未能保留split 的结果,因此您的长度测试是测试字符串的长度,而不是分号分隔的组件数
    3. (次要)不使用 with 语句,也不关闭文件,因此文件句柄可能会无限期打开(取决于 Python 解释器)

    固定代码:

    def get_quiz_list_handle_exceptions():
        success = True
        while success:
            try:
    
                file = input("Name of quiz-file: ")
                with open(file,'r') as file2:  # Use with statement to guarantee file is closed
                    for lines in range(0,9):
                        quiz_line = file2.readline()
                        quiz_line = quiz_line.split(";")
    
                        if len(quiz_line) != 4:
                            raise Exception
    
            # All your excepts/elses were insufficiently indented to match the try
            except FileNotFoundError as error:
                print("That resulted in an input/output error, please try again!", error)
    
            except Exception:
                print("The file is not on the proper format. There needs to be four strings, separated by ; in each line of the file.")
            else:
                success = False  # Fixed indent
    
    
    get_quiz_list_handle_exceptions()
    

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 2022-07-28
      • 1970-01-01
      • 2020-12-10
      • 2017-11-03
      • 2013-09-06
      • 2020-12-04
      • 2012-07-12
      • 2013-01-21
      相关资源
      最近更新 更多