Lloyd = {
"name":"Lloyd",
"homework": [90,97,75,92],
"quizzes": [ 88,40,94],
"tests": [ 75,90]
}
Alice = {
"name":"Alice",
"homework": [100,92,98,100],
"quizzes": [82,83,91],
"tests": [89,97]
}
Tyler = {
"name":"Tyler",
"homework": [0,87,75,22],
"quizzes": [0,75,78],
"tests": [100,100]
}
def average(value):
avg=0
items = len(value)
for item in value:
avg +=item
return avg/items
def getAverage(dictin):
hw = average(dictin.get('homework'))
quiz = average(dictin.get('quizzes'))
tests = average(dictin.get('tests'))
weighted_average = hw*.1 + quiz*.3 + tests*.6
return weighted_average
def getLetterGrade(score):
if score >=90:
return "A"
elif score < 90 and score >= 80:
return "B"
elif score < 80 and score >= 70:
return "C"
elif score < 70 and score >= 60:
return "D"
elif score < 60:
return "F"
else:
return "No grades for you"
score = getAverage(Lloyd)
grade = getLetterGrade(score)
print grade
这很好用,但我被告知在分数为 89.5 的情况下,它不起作用。我也尝试过,但我无法找出问题所在。欢迎任何错误。