程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python class and try exception problems

編輯:Python

Questions in notes

class StudentsDataException(Exception): pass # there pass yes pass all exception, Why do you need the following code 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 This class What is the specific role of , Can you explain the function of each code 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)# Can you explain (i+1,line) What's the situation  student = columns[0] + ' ' + columns[1] try: points = float(columns[2]) except ValueError: raise BadLine(i + 1, line) try: #print(data[student]), here print Nothing at all , Just want to see if it can print what  data[student] += points# Want to know data[student] Start is not 0 except KeyError: data[student] = points#keyerror There is no mapping object , there “data[student] = points” What does it mean  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")""" The contents of the document :John Smith 5Anna Boleyn 4.5John Smith 2Anna Boleyn 11Andrew Cox 1.5 final output yes Andrew Cox 1.5Anna Boleyn 15.5John Smith 7.0"""

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved