悊知文本文件chengjibiao.txt悍存放着170名学生的姓名和成绩(附件下载chengjibiao.txt), 请编写程序从该文件悍读出数据悇行分析,得出如下汇总结果悋出,并存入到文本文件cjhz.txt悍。 1、求出总平均分; 2、优秀悌数(成绩>=90); 3、不及格悌数(成绩<60); 4、最高分学生姓名及成绩; 5、最低分学生姓名及成绩。
时间: 2024-02-20 16:01:25 浏览: 17
好的,我会根据您的要求进行编程。以下是程序代码:
```python
# 读取文件
with open('chengjibiao.txt', 'r') as f:
lines = f.readlines()
# 处理数据
total_score = 0 # 总成绩
num_of_students = len(lines) # 学生总数
num_of_excellent = 0 # 优秀数量
num_of_fail = 0 # 不及格数量
max_score = 0 # 最高分
min_score = 100 # 最低分
max_student = '' # 最高分学生姓名
min_student = '' # 最低分学生姓名
for line in lines:
name, score = line.strip().split()
score = int(score)
# 计算总成绩
total_score += score
# 统计优秀和不及格数量
if score >= 90:
num_of_excellent += 1
if score < 60:
num_of_fail += 1
# 更新最高分和最低分
if score > max_score:
max_score = score
max_student = name
if score < min_score:
min_score = score
min_student = name
# 计算平均分
average_score = total_score / num_of_students
# 写入结果到文件
with open('cjhz.txt', 'w') as f:
f.write('总平均分: {:.2f}\n'.format(average_score))
f.write('优秀人数: {}\n'.format(num_of_excellent))
f.write('不及格人数: {}\n'.format(num_of_fail))
f.write('最高分学生: {} {}\n'.format(max_student, max_score))
f.write('最低分学生: {} {}\n'.format(min_student, min_score))
```
您可以将该代码保存为一个.py文件,然后在终端中运行该文件即可得到结果。注意确保文件名和路径正确无误。