输入学号查成绩Python
时间: 2024-03-28 09:32:39 浏览: 30
当然可以!输入学号查成绩的功能可以通过Python编程来实现。下面是一个简单的示例代码:
```python
# 定义一个字典,存储学生的学号和成绩
scores = {
"20210001": 90,
"20210002": 85,
"20210003": 95,
# 可以继续添加更多的学号和成绩
}
# 定义一个函数,用于根据学号查找成绩
def find_score(student_id):
if student_id in scores:
return scores[student_id]
else:
return "学号不存在"
# 输入学号
student_id = input("请输入学号:")
# 调用函数查找成绩并输出结果
result = find_score(student_id)
print("成绩为:", result)
```
在这个示例代码中,我们使用了一个字典来存储学生的学号和成绩。然后定义了一个函数`find_score`,该函数接受一个学号作为参数,通过查找字典中对应的值来返回成绩。最后,我们通过输入学号并调用函数来查找并输出成绩。
相关问题
python输入学号姓名成绩排序
可以使用Python的内置函数sorted()进行排序,以下是一个示例代码:
```python
# 定义学生信息列表
students = [
{'name': 'Tom', 'id': '001', 'score': 90},
{'name': 'Jerry', 'id': '002', 'score': 80},
{'name': 'Alice', 'id': '003', 'score': 95},
]
# 按照分数进行排序
sorted_students = sorted(students, key=lambda student: student['score'], reverse=True)
# 输出排序后的学生信息
for student in sorted_students:
print('name: {}, id: {}, score: {}'.format(student['name'], student['id'], student['score']))
```
输出结果为:
```
name: Alice, id: 003, score: 95
name: Tom, id: 001, score: 90
name: Jerry, id: 002, score: 80
```
在本代码中,我们先定义了一个包含学生信息的列表students,其中每个学生信息包括姓名、学号和成绩。然后我们使用内置的sorted()函数对学生信息进行排序,排序的关键字为学生的成绩score,降序排列(reverse=True)。最后遍历排序后的学生信息列表,并输出每个学生的姓名、学号和成绩。
python写一个学号查询和成绩排序系统
好的,这里给出一个简单的 Python 学号查询和成绩排序系统的代码示例。这个系统可以通过输入学号查询学生的成绩,并按照成绩从高到低排序。
假设有一个学生成绩字典,其中键为学号,值为对应学生的成绩,可以使用 Python 的字典进行存储和查询。代码如下:
```python
# 学生成绩字典,键为学号,值为对应学生的成绩
scores = {'001': 90, '002': 85, '003': 92, '004': 88, '005': 95}
# 查询学号对应的成绩
def query_score(score_dict, student_id):
if student_id in score_dict:
return score_dict[student_id]
else:
return None
# 根据成绩从高到低排序
def sort_scores(score_dict):
return sorted(score_dict.items(), key=lambda x: x[1], reverse=True)
# 测试查询和排序功能
print('学号查询和成绩排序系统')
while True:
print('请输入学号(输入 q 退出):')
student_id = input().strip()
if student_id == 'q':
break
# 查询成绩
score = query_score(scores, student_id)
if score is None:
print('未找到对应学号的成绩。')
else:
print('学号 %s 的成绩是 %d。' % (student_id, score))
# 排序成绩
sorted_scores = sort_scores(scores)
print('成绩排名:')
for i, (student_id, score) in enumerate(sorted_scores):
print('%d. 学号 %s,成绩 %d' % (i+1, student_id, score))
```
这个系统使用了两个函数:query_score 函数用于查询学号对应的成绩,sort_scores 函数用于按照成绩从高到低排序。在测试代码中,使用一个 while 循环不断询问用户输入学号,并输出查询和排序结果。
输出结果示例:
```
学号查询和成绩排序系统
请输入学号(输入 q 退出):
001
学号 001 的成绩是 90。
成绩排名:
1. 学号 005,成绩 95
2. 学号 003,成绩 92
3. 学号 001,成绩 90
4. 学号 004,成绩 88
5. 学号 002,成绩 85
请输入学号(输入 q 退出):
006
未找到对应学号的成绩。
成绩排名:
1. 学号 005,成绩 95
2. 学号 003,成绩 92
3. 学号 001,成绩 90
4. 学号 004,成绩 88
5. 学号 002,成绩 85
请输入学号(输入 q 退出):
q
```
希望这个例子可以帮助你实现一个简单的学号查询和成绩排序系统。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)