1现有22级大数据班有其中6位同学的个人信息(学号+姓名+Python的作业成绩)如下: 220309011 xiaorun 78 220309001 xiaotan 80 220309010 xiaozhang 82 220309012 xiaoyang74 220309015 xiaochen84 220309016 xiaoli 77 要求编程实现:学生输入上述某同学学号后输出其名字和个人成 绩以及成绩所在的名次,如果输入的学号不在字典中则输出字符 串“Not Found”。
时间: 2024-03-28 19:40:08 浏览: 13
好的,我来为您解答。这里给出一个Python字典来存储这些学生的信息:
```python
students = {
"220309011": {"name": "xiaorun", "score": 78},
"220309001": {"name": "xiaotan", "score": 80},
"220309010": {"name": "xiaozhang", "score": 82},
"220309012": {"name": "xiaoyang", "score": 74},
"220309015": {"name": "xiaochen", "score": 84},
"220309016": {"name": "xiaoli", "score": 77}
}
```
接下来,可以使用以下代码来实现您的要求:
```python
# 获取学生信息
def get_student_info(students, id):
if id in students:
# 从字典中获取学生信息
student = students[id]
# 获取学生的分数
score = student["score"]
# 计算学生的排名
rank = sorted([s["score"] for s in students.values()], reverse=True).index(score) + 1
# 输出学生信息和排名
print("Name: {}, Score: {}, Rank: {}".format(student["name"], score, rank))
else:
print("Not Found")
# 测试
get_student_info(students, "220309011")
get_student_info(students, "220309002")
```
运行结果:
```
Name: xiaorun, Score: 78, Rank: 4
Not Found
```
上述代码通过 `get_student_info` 函数来获取学生的信息,参数 `students` 是存储学生信息的字典,参数 `id` 是要查询的学生的学号。如果该学号存在于字典中,则输出该学生的姓名、分数和排名;否则输出字符串 "Not Found"。