下面是我分配的规则:您可以访问字典格式的student_scores数据库。student_scores中的键是学生的名字,值是他们的考试成绩。
编写一个将分数转换成分数的程序。在程序结束时,您应该有一个名为student_grades的新字典,它应该包含学生的键名和值的分数。将检查student_grades字典的最终版本。
不要修改第1-7行以更改现有的student_scores字典。
这是评分标准:
91-100分:等级=“优秀”
81-90分:年级=“超出预期”
71-80分:等级=“可接受”
分数70分或更低:分数=“不及格”
预期产出{“哈利”:“超出预期”,“罗恩”:“可接受”,“赫敏”:“杰出”,“德拉科”:“可接受”,“内维尔”:“失败”}
这是我的代码:
student_scores = {
"Harry": 81,
"Ron": 78,
"Hermione": 99,
"Draco": 74,
"Neville": 62,
}
# Don't change the code above
#TODO-1: Create an empty dictionary called "student_grades."
#TODO-2: Write your code below to append the grades to student_grades.
student_grades = {}
gradeslist = []
for student in student_scores:
if student_scores[student] <= 100 and student_scores[student] >= 91:
gradeslist.append("Outstanding")
elif student_scores[student] <= 90 and student_scores[student] >= 81:
gradeslist.append("Exceeds Expectations")
elif student_scores[student] <= 80 and student_scores[student] >= 71:
gradeslist.append("Acceptable")
else:
gradeslist.append("Fail")
for key in student_scores:
for num in range(0, len(student_scores)-1):
student_grades[key] = gradeslist[num]
# Don't change the code below
print(student_grades)输出{“哈利”:“可接受”,“罗恩”:“可接受”,“赫敏”:“可接受”,“德拉科”:“可接受”,“内维尔”:“可接受”}
为什么会发生这种事?
发布于 2022-03-01 23:07:39
for key in student_scores:
for num in range(0, len(student_scores)-1):
student_grades[key] = gradeslist[num] 这个循环将每个学生设置为gradeslist中的第一个值,然后将每个学生设置为gradeslist中的第二个值,依此类推。
顺便提一句,他们都得到了“可接受”而不是“失败”,因为range()返回的间隔包括起始号,而不是结束号,所以range(0, len(student_scores)-1)产生了0-3的值。
https://stackoverflow.com/questions/71315096
复制相似问题