您的位置:首页 > 其它

except as a control flow mechanism

2015-07-08 12:45 387 查看
#quote from MIT 'introduction to computation and programming using python, Revised'
def getGrades(fname):
try:
gradesFile = open(fname, 'r') #open file for reading
except:
raise ValueError('getGrades could not open ' + fname)
grades = []
for line in gradesFile:
try:
grades.append(float(line))
except:
raise ValueError('Unable to convert line to float')
return grades

try:
grades = getGrades('quiz.txt')
grades.sort()
median = grades[len(grades)//2]
print 'Median grade is', median
except ValueError, errorMsg:
print 'Whoops.', errorMsg
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: