用Python写一个学生管理项目,该项目是通过面向对象的方法设计学生类Student,包含一个学生编号(No)、学生姓名(Name)性别(Sex)、年龄(Age),然后设计学生记录管理类StudentList来管理一组学生记录,并且控制台输入1显示所有学生信息,输入2增加一个学生,输入3删除学生信息,输入4更新学生信息,输入5退出系统。然后显示所有学生的时候按表格输出
时间: 2024-02-15 16:04:28 浏览: 127
好的,我可以帮你写一个简单的学生管理项目,以下是代码:
```python
class Student:
def __init__(self, No, Name, Sex, Age):
self.No = No
self.Name = Name
self.Sex = Sex
self.Age = Age
class StudentList:
def __init__(self):
self.students = []
def add_student(self, No, Name, Sex, Age):
student = Student(No, Name, Sex, Age)
self.students.append(student)
def delete_student(self, No):
for student in self.students:
if student.No == No:
self.students.remove(student)
return True
return False
def update_student(self, No, Name, Sex, Age):
for student in self.students:
if student.No == No:
student.Name = Name
student.Sex = Sex
student.Age = Age
return True
return False
def show_students(self):
print("{:<10}{:<10}{:<10}{:<10}".format("No", "Name", "Sex", "Age"))
for student in self.students:
print("{:<10}{:<10}{:<10}{:<10}".format(student.No, student.Name, student.Sex, student.Age))
def main():
student_list = StudentList()
while True:
print("1. Show all students")
print("2. Add a student")
print("3. Delete a student")
print("4. Update a student")
print("5. Exit")
choice = input("Please enter your choice (1-5): ")
if choice == "1":
student_list.show_students()
elif choice == "2":
No = input("Please enter student No: ")
Name = input("Please enter student Name: ")
Sex = input("Please enter student Sex: ")
Age = input("Please enter student Age: ")
student_list.add_student(No, Name, Sex, Age)
elif choice == "3":
No = input("Please enter student No: ")
if student_list.delete_student(No):
print("Delete successfully!")
else:
print("No such student!")
elif choice == "4":
No = input("Please enter student No: ")
Name = input("Please enter student Name: ")
Sex = input("Please enter student Sex: ")
Age = input("Please enter student Age: ")
if student_list.update_student(No, Name, Sex, Age):
print("Update successfully!")
else:
print("No such student!")
elif choice == "5":
break
else:
print("Invalid input, please try again.")
if __name__ == "__main__":
main()
```
你可以在控制台中运行该程序,根据提示输入相应的操作选项进行学生信息管理。当选择显示所有学生信息时,输出的格式为表格形式。
阅读全文