提問在注釋裡
class StudentsDataException(Exception): pass #這裡的pass是pass所有exception,下面代碼為何還要raise exception?class BadLine(StudentsDataException): def __init__(self, line_number, line_string): super().__init__(self) self.line_number = line_number self.line_string = line_string這個class的具體作用是什麼,能否解釋一下每條代碼的作用class FileEmpty(StudentsDataException): def __init__(self): super().__init__(self)from os import strerrordata = { }file_name = input("Enter student's data filename: ")line_number = 1try: f = open(filename, "rt") lines = f.readlines() f.close() if len(lines) == 0: raise FileEmpty() for i in range(len(lines)): line = lines[i] columns = line.split() if len(columns) != 3: raise BadLine(i + 1, line)#能否解釋一下(i+1,line)是什麼情況 student = columns[0] + ' ' + columns[1] try: points = float(columns[2]) except ValueError: raise BadLine(i + 1, line) try: #print(data[student]),這裡print根本沒有顯示任何內容,只是想看一下能print什麼 data[student] += points#想知道data[student]起始是不是0 except KeyError: data[student] = points#keyerror的話沒有映射對象,這裡的“data[student] = points”代表什麼意思 for student in sorted(data.keys()): print(student,'\t', data[student])except IOError as e: print("I/O error occurred: ", strerror(e.errno))except BadLine as e: print("Bad line #" + str(e.line_number) + " in source file:" + e.line_string)except FileEmpty: print("Source file empty")"""文件裡的內容:John Smith 5Anna Boleyn 4.5John Smith 2Anna Boleyn 11Andrew Cox 1.5最後的output是Andrew Cox 1.5Anna Boleyn 15.5John Smith 7.0"""
Catalog Data specifications
報錯內容如下:TemplateSyntaxError at