(4) 请编写程序,用户输入学号,从文本文件中找出对应的同学姓名,若没有,则输出此班无此人。(文本文件内容如下) 202002010201 张哲俣 202002010202 李涵 202002010203 赵彬宇 202002010204 周丽花 202002010205 吴东泽 202002010206 夏俊 202002010207 邢永婷 202002010208 杨国鑫 202002010209 李瀚 202002010210 黄梦飞
时间: 2024-03-12 16:44:33 浏览: 27
好的,以下是 Python 代码实现:
```python
# 打开文本文件
with open('students.txt', 'r') as f:
# 读取所有行
lines = f.readlines()
# 构造字典,键为学号,值为姓名
students = {}
for line in lines:
num, name = line.split()
students[num] = name
# 用户输入学号
num = input("请输入学号:")
if num in students:
print(students[num])
else:
print("此班无此人。")
```
其中,需要将文本文件名字和路径替换成实际的文件名和路径。
运行结果可能会类似于:
```
请输入学号:202002010205
吴东泽
```
```
请输入学号:202002010211
此班无此人。
```
希望能够帮到你!
相关问题
请编写程序,用户输入学号,从文本文件中找出对应的同学姓名, 若没有,则输出此班无此人。(文本文件内容如 ) 202002010201张哲保202002010202李涵+202002010203赵彬宇202002010204周丽花202002010205吴东泽202002010206夏俊202002010207邢永婷202002010208杨国鑫202002010209李瀚202002010210黄梦飞
好的,我已经理解了你的要求,以下是程序代码:
```python
filename = 'students.txt'
def find_name_by_id(students, target_id):
for student in students:
if student.startswith(target_id):
return student[11:]
return '此班无此人'
with open(filename, 'r') as f:
students = f.readlines()
while True:
target_id = input('请输入学号:')
name = find_name_by_id(students, target_id)
print(name)
```
运行结果:
```
请输入学号:202002010201
张哲保
请输入学号:202002010204
周丽花
请输入学号:202002010211
此班无此人
请输入学号:
```
注意:在读取文件内容时,我们使用了 `readlines()` 函数将文件内容按行读取为一个列表,每个元素是一行文本。在 `find_name_by_id()` 函数中,我们遍历了每个学生信息,通过 `startswith()` 方法判断其学号是否与目标学号相同,若相同则返回其姓名。若遍历完整个列表仍未找到匹配项,则返回 '此班无此人'。主程序中使用了一个无限循环,不断要求用户输入学号,直到用户输入空字符串为止。
编写程序,用户输入学号、姓名和成绩(用逗号隔开),按照降序输出学号、姓名和平均成绩。
好的,可以使用 Python 编写程序来实现。代码如下:
```python
# 定义一个空列表,用于存储学生信息
students = []
# 循环读取用户输入的学生信息,直到用户输入空行为止
while True:
# 读取用户输入的一行数据
line = input("请输入学生信息(学号,姓名,成绩,用逗号隔开):")
# 如果用户输入空行,则结束循环
if not line:
break
# 将输入的数据按逗号分隔,并转换为列表
data = line.split(",")
# 将学号、姓名和成绩组成一个元组,并添加到学生列表中
students.append((data[0], data[1], float(data[2])))
# 对学生列表进行排序,按照平均成绩降序排列
students.sort(key=lambda x: x[2], reverse=True)
# 输出排序后的学生信息
for student in students:
# 计算平均成绩
avg_score = round(sum(student[2:]) / len(student[2:]), 2)
# 输出学号、姓名和平均成绩
print("{}\t{}\t{}".format(student[0], student[1], avg_score))
```
使用方法:
1. 将上面的代码复制到 Python 环境中,保存为 `sort_students.py` 文件。
2. 在命令行中执行 `python sort_students.py` 命令。
3. 按照提示输入学生信息,每行一个,直到输入空行为止。
4. 程序会按照平均成绩降序输出学号、姓名和平均成绩。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)