写一个pythonshixian当前目录下有一个文件名为score1.txt的文本文件, 存放着某班学生的Python课程成绩,共有学号、平时成绩、期末成绩三列。请根据平时成绩占30%,期末成绩占70%的比例计算总评成绩(取整数),并分学号、总评成绩两列写入另一文件score2.txt。 同时在屏幕上输出学生总人数,按总评成绩计90以上 、80~89、70~79、60~69、60分以下各成绩档的人数和班级总平均分(取整数)。
时间: 2023-06-24 13:05:15 浏览: 370
```python
# 打开score1.txt文件,读取数据
with open('score1.txt', 'r') as f:
lines = f.readlines()
# 定义成绩档次的人数统计变量
count_90_above = 0
count_80_89 = 0
count_70_79 = 0
count_60_69 = 0
count_below_60 = 0
# 定义总分和学生人数统计变量
total_score = 0
count_students = 0
# 遍历每一行,计算总评成绩并写入score2.txt文件,同时统计各档次人数和总平均分
with open('score2.txt', 'w') as f:
for line in lines:
# 分割每一行的数据
id, daily_score, final_score = line.split()
# 计算总评成绩
total_score = int(daily_score) * 0.3 + int(final_score) * 0.7
# 写入score2.txt文件
f.write(f'{id}\t{int(total_score)}\n')
# 统计各档次人数
if total_score >= 90:
count_90_above += 1
elif total_score >= 80:
count_80_89 += 1
elif total_score >= 70:
count_70_79 += 1
elif total_score >= 60:
count_60_69 += 1
else:
count_below_60 += 1
# 统计总分和学生人数
total_score += int(total_score)
count_students += 1
# 计算班级总平均分
average_score = total_score // count_students
# 输出统计结果
print(f'总人数:{count_students}')
print(f'90分以上的人数:{count_90_above},占比:{count_90_above / count_students * 100:.2f}%')
print(f'80~89分的人数:{count_80_89},占比:{count_80_89 / count_students * 100:.2f}%')
print(f'70~79分的人数:{count_70_79},占比:{count_70_79 / count_students * 100:.2f}%')
print(f'60~69分的人数:{count_60_69},占比:{count_60_69 / count_students * 100:.2f}%')
print(f'60分以下的人数:{count_below_60},占比:{count_below_60 / count_students * 100:.2f}%')
print(f'班级总平均分:{average_score}')
```