学生信息管理系统python代码
pythonclass Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, name, age, grade):
student = Student(name, age, grade)
self.students.append(student)
def find_student(self, name):
for student in self.students:
if student.name == name:
return student
return None
def remove_student(self, name):
student = self.find_student(name)
if student:
self.students.remove(student)
print(f"Student {name} removed successfully.")
else:
print(f"Student {name} not found.")
def list_students(self):
print("List of Students:")
for student in self.students:
print(f"Name: {student.name}, Age: {student.age}, Grade: {student.grade}")
def main():
student_manager = StudentManager()
while True:
print("\nStudent Management System")
print("1. Add Student")
print("2. Find Student")
print("3. Remove Student")
print("4. List Students")
print("5. Quit")
choice = input("Enter your choice: ")
if choice == '1':
name = input("Enter student's name: ")
age = input("Enter student's age: ")
grade = input("Enter student's grade: ")
student_manager.add_student(name, age, grade)
elif choice == '2':
name = input("Enter student's name to find: ")
student = student_manager.find_student(name)
if student:
print(f"Student found - Name: {student.name}, Age: {student.age}, Grade: {student.grade}")
else:
print("Student not found.")
elif choice == '3':
name = input("Enter student's name to remove: ")
student_manager.remove_student(name)
elif choice == '4':
student_manager.list_students()
elif choice == '5':
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
这个程序包含两个类:Student
和 StudentManager
。Student
类表示一个学生对象,而 StudentManager
类包含对学生对象的管理方法,如添加、查找、删除和列出。
在 main
函数中,程序提供一个简单的文本菜单,让用户选择要执行的操作。根据用户的选择,程序调用相应的方法来处理学生信息。
pythonclass Student:
def __init__(self, name, age, grade):
self.name = name
self.age = age
self.grade = grade
class StudentManager:
def __init__(self):
self.students = []
def add_student(self, name, age, grade):
student = Student(name, age, grade)
self.students.append(student)
def find_student(self, name):
for student in self.students:
if student.name == name:
return student
return None
def remove_student(self, name):
student = self.find_student(name)
if student:
self.students.remove(student)
print(f"Student {name} removed successfully.")
else:
print(f"Student {name} not found.")
def list_students(self):
print("List of Students:")
for student in self.students:
print(f"Name: {student.name}, Age: {student.age}, Grade: {student.grade}")
def main():
student_manager = StudentManager()
while True:
print("\nStudent Management System")
print("1. Add Student")
print("2. Find Student")
print("3. Remove Student")
print("4. List Students")
print("5. Quit")
choice = input("Enter your choice: ")
if choice == '1':
name = input("Enter student's name: ")
age = input("Enter student's age: ")
grade = input("Enter student's grade: ")
try:
age = int(age)
student_manager.add_student(name, age, grade)
except ValueError:
print("Age must be a valid integer.")
elif choice == '2':
name = input("Enter student's name to find: ")
student = student_manager.find_student(name)
if student:
print(f"Student found - Name: {student.name}, Age: {student.age}, Grade: {student.grade}")
else:
print("Student not found.")
elif choice == '3':
name = input("Enter student's name to remove: ")
student_manager.remove_student(name)
elif choice == '4':
student_manager.list_students()
elif choice == '5':
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
在这个补充部分中,我们使用了 try-except
结构来捕获用户输入的年龄是否为整数的异常,并给出相应的提示信息。这样可以确保用户输入的年龄是一个有效的整数。