2. 键盘输入6名学生信息(学号、姓名、专业、性别、班级、出生年月日、两门课程的成绩),计算每名学生总分,将原有数据和平均分存放在磁盘文件JXXYstu.txt中。
时间: 2023-10-20 16:21:55 浏览: 48
这是一个简单的Python实现:
```python
# 定义学生类
class Student:
def __init__(self, id, name, major, gender, class_name, birthday, score1, score2):
self.id = id
self.name = name
self.major = major
self.gender = gender
self.class_name = class_name
self.birthday = birthday
self.score1 = score1
self.score2 = score2
# 计算总分
def total_score(self):
return self.score1 + self.score2
# 存储学生信息的列表
students = []
# 输入6名学生信息
for i in range(6):
id = input("请输入学号:")
name = input("请输入姓名:")
major = input("请输入专业:")
gender = input("请输入性别:")
class_name = input("请输入班级:")
birthday = input("请输入出生年月日:")
score1 = float(input("请输入第一门课程成绩:"))
score2 = float(input("请输入第二门课程成绩:"))
student = Student(id, name, major, gender, class_name, birthday, score1, score2)
students.append(student)
# 计算总分和平均分
total_score = 0
for student in students:
total_score += student.total_score()
average_score = total_score / len(students)
# 写入文件
with open("JXXYstu.txt", "w") as f:
for student in students:
f.write(f"{student.id} {student.name} {student.major} {student.gender} {student.class_name} {student.birthday} {student.score1} {student.score2} {student.total_score()} {average_score}\n")
```
运行程序后,会提示你输入6名学生的信息,然后程序会计算每名学生的总分和平均分,并将原有数据和平均分存储在JXXYstu.txt文件中。