Python 3-分配等级
•定义一个函数,提示用户输入有效分数,直到他们输入哨兵值-999。让此函数创建并返回这些分数的列表。不要在列表中存储-999! •让main()将该列表传递给第二个函数,该函数遍历分数列表,并将其与相应的分数一起打印 我在使用getGrade函数时遇到问题,它在grades中为I提供了错误:未定义名称“grades”Python 3-分配等级,python,python-3.x,for-loop,main,Python,Python 3.x,For Loop,Main,•定义一个函数,提示用户输入有效分数,直到他们输入哨兵值-999。让此函数创建并返回这些分数的列表。不要在列表中存储-999! •让main()将该列表传递给第二个函数,该函数遍历分数列表,并将其与相应的分数一起打印 我在使用getGrade函数时遇到问题,它在grades中为I提供了错误:未定义名称“grades” def main(): grade = getScore() print(getGrade(grade)) def getScore(): grades = [
def main():
grade = getScore()
print(getGrade(grade))
def getScore():
grades = []
score = int(input("Enter grades (-999 ends): "))
while score != -999:
grades.append(score)
score = int(input("Enter grades (-999 ends): "))
return grades
def getGrade(score):
best = 100
for i in grades:
if score >= best - 10:
print(" is an A")
elif score >= best - 20:
print(score, " is a B")
elif score >= best - 30:
print(score, " is a C")
elif score >= best - 40:
print(score, " is a D")
else:
print(score, "is a F")
main()
您将函数定义为
getScore()
,但您正在调用getScores()
,因此正如预期的那样,这会引发错误,因为您调用的函数实际上不存在/尚未定义
附录:自从你修改了你的问题,在修正了之前的错误之后
同样,您正在调用
grades
,但是grades
是在您试图迭代grades
的函数范围之外的其他函数中定义的,您的函数名为getScore
,而不是getScores