我正在尝试用Python编写一个平均成绩计算器,用于CodeAcademy课程。我一直收到这个错误
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
关于平均函数total = sum(numbers)
。我理解这意味着我试图将两种不同的类型相加,这是不可能的。但是我不知道这个NoneType
来自哪里,如果我试图只从给定字典中加入一个数字列表?
此外,编写此代码的更好方法是什么?我假设有更简单的方法可以完全相同。
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]
}
class_list = [lloyd, alice, tyler]
students = [lloyd, alice, tyler]
# Add your function below!
def average(numbers):
total = sum(numbers)
averege_grade = float(total)/len(numbers)
return averege_grade
def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
total_average = float(homework) * 1 + float(quizzes) * 3 + float(tests) * 6
def get_letter_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
elif score >= 60:
return "D"
else:
return "F"
def get_class_average(class_list):
results = []
for student in class_list:
resultadinho = get_average(student)
results.append(resultadinho)
return average(results)
print get_class_average(students)
print get_letter_grade
答案 0 :(得分:2)
您未从get_average()
返回任何内容,因此当sum
启动时(使用0
作为第一个累积值,它会尝试添加None
和{{ 1}}解释了错误信息。