用python写一个学生信息管理系统输入并存储学生的信息 、修改学生信息 、删除学生信息 、成绩排序、查找学生信息、进行测试
时间: 2023-12-21 22:07:04 浏览: 57
好的,以下是一个简单的学生信息管理系统的代码:
```python
class Student:
def __init__(self, name, age, gender, grade):
self.name = name
self.age = age
self.gender = gender
self.grade = grade
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
return True
return False
def find_student(self, name):
for student in self.students:
if student.name == name:
return student
return None
def sort_students_by_grade(self):
self.students.sort(key=lambda x: x.grade, reverse=True)
def display_students(self):
print("Name\tAge\tGender\tGrade")
for student in self.students:
print(f"{student.name}\t{student.age}\t{student.gender}\t{student.grade}")
if __name__ == "__main__":
sms = StudentManagementSystem()
while True:
print("\nStudent Management System Menu:")
print("1. Add student")
print("2. Remove student")
print("3. Find student")
print("4. Sort students by grade")
print("5. Display all students")
print("6. Quit")
choice = input("Enter your choice: ")
if choice == "1":
name = input("Enter student name: ")
age = int(input("Enter student age: "))
gender = input("Enter student gender: ")
grade = float(input("Enter student grade: "))
student = Student(name, age, gender, grade)
sms.add_student(student)
print(f"{name} has been added to the system.")
elif choice == "2":
name = input("Enter student name: ")
if sms.remove_student(name):
print(f"{name} has been removed from the system.")
else:
print(f"Could not find {name} in the system.")
elif choice == "3":
name = input("Enter student name: ")
student = sms.find_student(name)
if student:
print(f"Name: {student.name}")
print(f"Age: {student.age}")
print(f"Gender: {student.gender}")
print(f"Grade: {student.grade}")
else:
print(f"Could not find {name} in the system.")
elif choice == "4":
sms.sort_students_by_grade()
print("Students sorted by grade:")
sms.display_students()
elif choice == "5":
print("All students:")
sms.display_students()
elif choice == "6":
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
```
运行代码后,可以按照菜单提示进行操作。可以输入学生的姓名、年龄、性别和成绩,程序会将其保存到系统中。可以根据姓名查找、删除学生信息,也可以对学生信息按成绩排序。最后,可以显示所有学生信息或退出程序。
阅读全文