我正在尝试引用student_avg
函数中的letter_grade
函数,但似乎无法识别student_avg
参数。
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 average(numbers):
total = sum(numbers)
total = total/len(numbers)
return total
def student_avg(student):
"""Returns weighted average"""
homework = average(student['homework']) * 10/100
quizzes = average(student['quizzes']) * 30/100
tests = average(student['tests']) * 60/100
return homework + quizzes + tests
def lett_grade(studentname):
""" Gets the students letter grade A-F"""
if student_avg(studentname) >= 90:
print('A')
elif 90 > student_avg(studentname) >= 80:
print('B')
elif 80 > student_avg(studentname) >= 70:
print('C')
elif 70 > student_avg(studentname) >= 60:
print('D')
else:
print('F')
我收到错误:
Traceback (most recent call last):
File "/Users/Anthony/Documents/class.py", line 34, in <module>
if student_avg(studentname) >= 90:
NameError: name 'studentname' is not defined
另外,为什么我不应该在最后一个函数中缩进if
/ elif
语句?我在前两个函数中缩进了其他所有内容 - 它只是语法吗?
答案 0 :(得分:2)
您的问题是缩进。
在您的函数lett_grade(student name)
中,函数中的所有内容都是""" Gets the students letter grade A-F"""
。
因此,if
语句是函数的 out ,因此studentname
未定义。
以下是您编辑的代码:
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 average(numbers):
total = sum(numbers)
total = total/len(numbers)
return total
def student_avg(student):
"""Returns weighted average"""
homework = average(student['homework']) * 10/100
quizzes = average(student['quizzes']) * 30/100
tests = average(student['tests']) * 60/100
return homework + quizzes + tests
def lett_grade(studentname):
""" Gets the students letter grade A-F"""
if student_avg(studentname) >= 90:
print('A')
elif 90 > student_avg(studentname) >= 80:
print('B')
elif 80 > student_avg(studentname) >= 70:
print('C')
elif 70 > student_avg(studentname) >= 60:
print('D')
else:
print('F')