文件score.txt中存储了若干学生的姓名、学号和3门考试课的成绩。编程,将所有两门以上(含两门)课程不及格的学生信息输出到文件bad.txt、其他学生信息输出到pass.txt。假设两个空文件bad.txt、 pass.txt已经保存在py同名目录下。
时间: 2024-02-16 16:03:49 浏览: 25
好的,我可以帮您编写这个程序。以下是Python代码实现:
```python
with open('score.txt', 'r') as f:
lines = f.readlines()
bad_students = []
pass_students = []
for line in lines:
info = line.strip().split()
name, id, score1, score2, score3 = info
score1, score2, score3 = map(int, [score1, score2, score3])
if score1 < 60 and score2 < 60 and score3 < 60:
bad_students.append(line)
else:
pass_students.append(line)
with open('bad.txt', 'w') as f:
f.writelines(bad_students)
with open('pass.txt', 'w') as f:
f.writelines(pass_students)
```
以上代码首先读取score.txt文件中的所有行,然后遍历每一行,将每个学生的信息分离出来。如果该学生的三门课成绩均不及格,则将该行信息添加到bad_students列表中,否则添加到pass_students列表中。最后,将bad_students列表中的信息写入到bad.txt文件中,将pass_students列表中的信息写入到pass.txt文件中。