codecademy上的练习题,做完了,磕磕碰碰,感觉还是很生疏,笔记一下吧
3个字典就不多说了,数据范本
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
def get_letter_grade(score): #
根据分数打等级ABCDEF
if score >= 90:
return 'A'
elif score >=80:
return 'B'
elif score >=70:
return 'C'
elif score >=60:
return 'D'
else:
return 'F'
def average(numbers):# Add your function below! #注意了,这个函数是
用在最后算全班平均分用的....对其他功能没作用,别搞混了,当初写这个函数的时候
total = float(sum(numbers)) #被搞混了很久,还去写判断对象是不是list的语句,其实他只是针对最后出来3个人的平均分以后,算全班平均值的
result = total / float(len(numbers)) #课程里面让我们先写这个定义功能,我觉得可以放在最后写,以免误导.
return result
def get_average(student): #这个是用来算每个人的平均分的,student是变量,实际函数功能的里面,对象是value值.
homework=average(student['homework'])
quizzes=average(student['quizzes'])
tests=average(student['tests'])
total=0.1*homework+0.3*quizzes+0.6*tests #
每个分数占比不同的
return total
def get_class_average(students):
results = [] #这里的results,是用来记录3个人的平均分的,一个一个添加进去
for student in students:
results.append(get_average(student))
return average(results)
students=[lloyd,alice,tyler]
#printing class average
print (get_class_average(students)) #其实最后不用定义students,也能执行,写成
print (get_class_average([lloyd,alice,tyler]))
print (get_letter_grade(get_class_average(students))) #print (get_letter_grade(get_class_average([lloyd,alice,tyler])))