随意输入10个学生的姓名和成绩构成的字典,按照成绩大小排序。 (提示:循环输入数据存入空字典,用循环取值比较成绩大小,排序后输出结果) 例: Input name:wangInput score:90 Input name:zhangInput score:67 Input name:haiInput score:45Input name:zhouInput score:78Input name:jinInput score:89 Input name:panInput score:87 Input name:shuiInput score:78Input name:taiInput score:67 Input name:tianInput score:67Input name:ffInput score:56 order by score [('hai", '45'), ('ff, '56), ('zhang', '67), ('tian','67), ('tai', '67), ('zhou', '78'), ('shui, '78'),('pan', '87), (jin', '89'), (wang', '90')]
时间: 2023-07-16 15:12:23 浏览: 166
以下是代码实现:
```
# 初始化空字典
students = {}
# 循环输入数据
for i in range(10):
name = input("Input name:")
score = input("Input score:")
students[name] = int(score)
# 按成绩大小排序
sorted_students = sorted(students.items(), key=lambda x: x[1])
# 输出结果
print("order by score", sorted_students)
```
示例输出:
```
Input name:wang
Input score:90
Input name:zhang
Input score:67
Input name:hai
Input score:45
Input name:zhou
Input score:78
Input name:jin
Input score:89
Input name:pan
Input score:87
Input name:shui
Input score:78
Input name:tai
Input score:67
Input name:tian
Input score:67
Input name:ff
Input score:56
order by score [('hai', 45), ('ff', 56), ('zhang', 67), ('tai', 67), ('tian', 67), ('zhou', 78), ('shui', 78), ('pan', 87), ('jin', 89), ('wang', 90)]
```
阅读全文