请帮我用python语言编写一个完整的学生成绩管理系统,学生成绩信息包括:学期、学号、班别、姓名、四门课程成绩(语文、数学、英语和 Python),具体实现以下七个功能: (1)创建信息并以磁盘文件保...
时间: 2023-12-06 12:04:56 浏览: 44
好的,以下是一个简单的学生成绩管理系统的Python代码,实现了您所需的七个功能:
```python
import os
# 学生信息类
class Student:
def __init__(self, semester, student_id, class_name, name, chinese_score, math_score, english_score, python_score):
self.semester = semester
self.student_id = student_id
self.class_name = class_name
self.name = name
self.chinese_score = chinese_score
self.math_score = math_score
self.english_score = english_score
self.python_score = python_score
# 学生成绩管理系统类
class StudentScoreManagementSystem:
def __init__(self):
self.students = []
self.file_name = 'students.txt'
# 创建信息并以磁盘文件保存
def create_student(self):
semester = input("请输入学期:")
student_id = input("请输入学号:")
class_name = input("请输入班别:")
name = input("请输入姓名:")
chinese_score = input("请输入语文成绩:")
math_score = input("请输入数学成绩:")
english_score = input("请输入英语成绩:")
python_score = input("请输入Python成绩:")
student = Student(semester, student_id, class_name, name, chinese_score, math_score, english_score, python_score)
self.students.append(student)
self.save_students_to_file()
# 读取磁盘文件中的学生信息
def read_students_from_file(self):
if os.path.exists(self.file_name):
with open(self.file_name, 'r') as f:
lines = f.readlines()
for line in lines:
items = line.strip().split(',')
student = Student(items[0], items[1], items[2], items[3], items[4], items[5], items[6], items[7])
self.students.append(student)
# 将学生信息保存到磁盘文件中
def save_students_to_file(self):
with open(self.file_name, 'w') as f:
for student in self.students:
f.write('{},{},{},{},{},{},{},{}\n'.format(student.semester, student.student_id, student.class_name, student.name, student.chinese_score, student.math_score, student.english_score, student.python_score))
# 根据学号查询学生信息
def query_student_by_id(self):
student_id = input("请输入学号:")
for student in self.students:
if student.student_id == student_id:
print("学期:{},学号:{},班别:{},姓名:{},语文成绩:{},数学成绩:{},英语成绩:{},Python成绩:{}".format(student.semester, student.student_id, student.class_name, student.name, student.chinese_score, student.math_score, student.english_score, student.python_score))
# 根据班别查询学生信息
def query_student_by_class(self):
class_name = input("请输入班别:")
for student in self.students:
if student.class_name == class_name:
print("学期:{},学号:{},班别:{},姓名:{},语文成绩:{},数学成绩:{},英语成绩:{},Python成绩:{}".format(student.semester, student.student_id, student.class_name, student.name, student.chinese_score, student.math_score, student.english_score, student.python_score))
# 根据姓名查询学生信息
def query_student_by_name(self):
name = input("请输入姓名:")
for student in self.students:
if student.name == name:
print("学期:{},学号:{},班别:{},姓名:{},语文成绩:{},数学成绩:{},英语成绩:{},Python成绩:{}".format(student.semester, student.student_id, student.class_name, student.name, student.chinese_score, student.math_score, student.english_score, student.python_score))
# 修改学生信息
def modify_student(self):
student_id = input("请输入学号:")
for student in self.students:
if student.student_id == student_id:
student.semester = input("请输入学期:")
student.class_name = input("请输入班别:")
student.name = input("请输入姓名:")
student.chinese_score = input("请输入语文成绩:")
student.math_score = input("请输入数学成绩:")
student.english_score = input("请输入英语成绩:")
student.python_score = input("请输入Python成绩:")
self.save_students_to_file()
print("修改成功!")
# 删除学生信息
def delete_student(self):
student_id = input("请输入学号:")
for student in self.students:
if student.student_id == student_id:
self.students.remove(student)
self.save_students_to_file()
print("删除成功!")
# 显示所有学生信息
def show_all_students(self):
for student in self.students:
print("学期:{},学号:{},班别:{},姓名:{},语文成绩:{},数学成绩:{},英语成绩:{},Python成绩:{}".format(student.semester, student.student_id, student.class_name, student.name, student.chinese_score, student.math_score, student.english_score, student.python_score))
# 显示菜单
def show_menu(self):
print("=" * 30)
print("学生成绩管理系统")
print("1.创建信息并以磁盘文件保存")
print("2.根据学号查询学生信息")
print("3.根据班别查询学生信息")
print("4.根据姓名查询学生信息")
print("5.修改学生信息")
print("6.删除学生信息")
print("7.显示所有学生信息")
print("0.退出系统")
print("=" * 30)
# 运行系统
def run(self):
self.read_students_from_file()
while True:
self.show_menu()
choice = input("请输入您的选择:")
if choice == '1':
self.create_student()
elif choice == '2':
self.query_student_by_id()
elif choice == '3':
self.query_student_by_class()
elif choice == '4':
self.query_student_by_name()
elif choice == '5':
self.modify_student()
elif choice == '6':
self.delete_student()
elif choice == '7':
self.show_all_students()
elif choice == '0':
print("谢谢使用,再见!")
break
else:
print("输入有误,请重新输入!")
if __name__ == '__main__':
system = StudentScoreManagementSystem()
system.run()
```
注意:为了保证代码的正确性,需要在当前目录下创建一个名为`students.txt`的文本文件,用于保存学生信息。