我有以下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()
我没有读你的代码,因为它太长了,我被你想要的东西弄糊涂了,因为它是非描述性的(无意冒犯)。但你会问“我该如何破例?”您可以使用
Exception
作为超类(这是正确的术语吗?)。下面是一个例子:这应该是开始创建自定义异常的方法,您可以稍后了解更多。希望这有点帮助。在
编辑: 记住不要创建任何具有类似内置异常的自定义异常,例如
ZeroDivisionError
。在相关问题 更多 >
编程相关推荐