【发布时间】: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()
【问题讨论】:
-
您的
except子句缩进错误。将它们与try对齐。else之后的语句也是如此。添加四个空格。
标签: python for-loop exception raise