我有以下Python作业:
这就是文件的样子:
我被要求在计算中处理一些特殊情况。例如,当我有类似25/0的东西时,它将成为一个错误。我怎么能这样做?
这是我目前的代码:
import os.path
fLocation="//Users//Ivan//Desktop//"
print("Assumed file location is at: ", fLocation)
fName = input("\nPlease enter a file name with its extension (ex. XXX.txt): ")
fin = open(fLocation + fName, 'r')
fLinesList=fin.readlines()
fin.close()
print('*' * 50, "\nData items have been read from file:\n\t"
+ fLocation + fName + '\n')
print('-' * 30, "\nThe list of the lines in the file:\n", fLinesList)
print("\nThere are", len(fLinesList), "lines\n")
# Print file content line by line
print('\n' + '-' * 30 + '\nFile "' + fName + '" contains:\n')
print(fLinesList[0].strip())
print(fLinesList[1].strip())
print(fLinesList[2].strip())
print(fLinesList[3].strip())
print(fLinesList[4].strip())
print(fLinesList[5].strip())
wordsInLine4 = fLinesList[4].split()
print('-' * 30, "\nwords in line4 =", wordsInLine4)
QualityPointsprevious=float(wordsInLine4[6])
HoursAttemptedprevious=float(wordsInLine4[2])
GPAprevious=format(QualityPointsprevious/HoursAttemptedprevious,'.2f')
wordsInLine5 = fLinesList[5].split()
print('-' * 30, "\nwords in line5 =", wordsInLine5)
QualityPointsnow=float(wordsInLine5[6])
HoursAttemptednow=int(wordsInLine5[2])
GPAnow=format(QualityPointsnow/HoursAttemptednow,'.2f')
if (OverallHoursAttempted==0,OverallQualityPoints!=0):
OverallGPA==-1
else:
if (OverallHoursAttempted==0,OverallQualityPoints==0):
OverallGPA==0
else:
if (GPAprevious==-1):
OverallGPA==-1
OverallQualityPoints=QualityPointsprevious+QualityPointsnow
OverallHoursAttempted=HoursAttemptedprevious+HoursAttemptednow
OverallGPA=format(OverallQualityPoints/OverallHoursAttempted,'.2f')
QualityPointsprevious=str(QualityPointsprevious)
HoursAttemptedprevious=str(HoursAttemptedprevious)
QualityPointsnow=str(QualityPointsnow)
HoursAttemptednow=str(HoursAttemptednow)
save_path='C:\\TEMP\\'
text1=fLinesList[0]
text2=fLinesList[1]
text3=fLinesList[2]
text4=fLinesList[3]
text5=fLinesList[4]
text51=fLinesList[5]
text6="\n---------------------------"
text7="\nPrevious GPA:\t"+GPAprevious
text8="\nCurrent GPA:\t"+GPAnow
text9="\nOverall GPA:\t"+OverallGPA
text1=str(text1)
text2=str(text2)
text3=str(text3)
text4=str(text4)
text5=str(text5)
text51=str(text51)
text6=str(text6)
text7=str(text7)
text8=str(text8)
text9=str(text9)
wordsInLine0 = fLinesList[0].split()
print('-' * 30, "\nwords in line0 =", wordsInLine0)
lnameofstudent=wordsInLine0[2]
fnameofstudent=wordsInLine0[1]
lnameofstudent=str(wordsInLine0[2])
fnameofstudent=str(wordsInLine0[1])
name=fnameofstudent+" "+lnameofstudent
saveFile=open("C://TEMP//"+name+".txt","w")
saveFile.write(text1)
saveFile.write(text2)
saveFile.write(text3)
saveFile.write(text4)
saveFile.write(text5)
saveFile.write(text51)
saveFile.write(text6)
saveFile.write(text7)
saveFile.write(text8)
saveFile.write(text9)
saveFile.close()
答案 0 :(得分:0)
我没有阅读你的代码,因为它太长了,而且由于它是非描述性的(没有冒犯性)而被你想要的东西搞糊涂了。但你问"我怎么想做一个例外?"您可以使用Exception
作为超类来创建自定义异常(这是正确的术语吗?)。看一下这个例子:
class MyError(Exception): #exception names usually end with 'Error'
pass
这应该是开始制作自定义异常的方法,您可以稍后了解更多信息。希望这有点帮助。
修改强>:
切记不要创建具有类似内置异常的任何自定义异常,例如ZeroDivisionError
。