请帮我用python语言编写一个完整的学生成绩管理系统,学生成绩信息包括:学期、学号、班别、姓名、四门课程成绩(语文、数学、英语和 Python),具体实现以下七个功能: (1)创建信息并以磁盘文件保...
时间: 2023-12-26 07:04:38 浏览: 114
好的,以下是一个简单的学生成绩管理系统的Python代码实现:
```python
import os
# 定义学生类
class Student:
def __init__(self, term, sid, sclass, name, score):
self.term = term
self.sid = sid
self.sclass = sclass
self.name = name
self.score = score
# 定义学生成绩管理系统类
class GradeSystem:
def __init__(self):
self.students = []
# 添加学生信息
def add_student(self):
term = input("请输入学期:")
sid = input("请输入学号:")
sclass = input("请输入班别:")
name = input("请输入姓名:")
score = []
score.append(float(input("请输入语文成绩:")))
score.append(float(input("请输入数学成绩:")))
score.append(float(input("请输入英语成绩:")))
score.append(float(input("请输入Python成绩:")))
student = Student(term, sid, sclass, name, score)
self.students.append(student)
print("添加成功!")
# 查找学生成绩信息
def search_student(self):
sid = input("请输入要查找的学生学号:")
for student in self.students:
if student.sid == sid:
print("学期:", student.term)
print("学号:", student.sid)
print("班别:", student.sclass)
print("姓名:", student.name)
print("语文成绩:", student.score[0])
print("数学成绩:", student.score[1])
print("英语成绩:", student.score[2])
print("Python成绩:", student.score[3])
return
print("未找到该学生!")
# 修改学生成绩信息
def modify_student(self):
sid = input("请输入要修改的学生学号:")
for student in self.students:
if student.sid == sid:
student.term = input("请输入学期:")
student.sclass = input("请输入班别:")
student.name = input("请输入姓名:")
student.score[0] = float(input("请输入语文成绩:"))
student.score[1] = float(input("请输入数学成绩:"))
student.score[2] = float(input("请输入英语成绩:"))
student.score[3] = float(input("请输入Python成绩:"))
print("修改成功!")
return
print("未找到该学生!")
# 删除学生成绩信息
def delete_student(self):
sid = input("请输入要删除的学生学号:")
for student in self.students:
if student.sid == sid:
self.students.remove(student)
print("删除成功!")
return
print("未找到该学生!")
# 显示学生成绩信息
def show_students(self):
for student in self.students:
print("学期:", student.term)
print("学号:", student.sid)
print("班别:", student.sclass)
print("姓名:", student.name)
print("语文成绩:", student.score[0])
print("数学成绩:", student.score[1])
print("英语成绩:", student.score[2])
print("Python成绩:", student.score[3])
# 保存学生信息到磁盘文件
def save_to_file(self):
f = open("students.txt", "w")
for student in self.students:
f.write(student.term + "," + student.sid + "," + student.sclass + "," + student.name + "," + ",".join([str(s) for s in student.score]) + "\n")
f.close()
print("保存成功!")
# 加载学生信息
def load_from_file(self):
if os.path.exists("students.txt"):
f = open("students.txt", "r")
lines = f.readlines()
for line in lines:
fields = line.strip().split(",")
term = fields[0]
sid = fields[1]
sclass = fields[2]
name = fields[3]
score = [float(s) for s in fields[4:]]
student = Student(term, sid, sclass, name, score)
self.students.append(student)
f.close()
print("加载成功!")
else:
print("未找到学生信息文件!")
# 主程序
gs = GradeSystem()
while True:
print("""
1. 添加学生信息
2. 查找学生信息
3. 修改学生信息
4. 删除学生信息
5. 显示学生信息
6. 保存学生信息到磁盘文件
7. 加载学生信息
0. 退出程序
""")
choice = input("请输入菜单选项:")
if choice == "1":
gs.add_student()
elif choice == "2":
gs.search_student()
elif choice == "3":
gs.modify_student()
elif choice == "4":
gs.delete_student()
elif choice == "5":
gs.show_students()
elif choice == "6":
gs.save_to_file()
elif choice == "7":
gs.load_from_file()
elif choice == "0":
break
else:
print("无效的菜单选项!")
```
注意:该代码只是一个简单的示例,实际应用中需要进一步完善。
阅读全文