Python 编写学生通讯录管理系统。系统具有添加学生、删除学生、修改学生、查询学生、获取所有学生通讯信息及退出系统的功能。
时间: 2024-05-12 18:17:09 浏览: 11
好的,可以为您提供一个简单的 Python 学生通讯录管理系统的代码示例:
```python
class Student:
def __init__(self, name, email, phone):
self.name = name
self.email = email
self.phone = phone
class ContactList:
def __init__(self):
self.student_list = []
def add_student(self, student):
self.student_list.append(student)
def delete_student(self, name):
for student in self.student_list:
if student.name == name:
self.student_list.remove(student)
def modify_student(self, name, email, phone):
for student in self.student_list:
if student.name == name:
student.email = email
student.phone = phone
def get_student(self, name):
for student in self.student_list:
if student.name == name:
return (student.name, student.email, student.phone)
def get_all_students(self):
for student in self.student_list:
print(f"Name: {student.name}, Email: {student.email}, Phone: {student.phone}")
def main():
contact_list = ContactList()
while True:
print("""
1. Add a student
2. Delete a student
3. Modify a student
4. Get student
5. Get all students
6. Exit
""")
choice = input("Enter your choice: ")
if choice == "1":
name = input("Enter name: ")
email = input("Enter email: ")
phone = input("Enter phone: ")
contact_list.add_student(Student(name, email, phone))
print("Student added successfully!")
elif choice == "2":
name = input("Enter name: ")
contact_list.delete_student(name)
print("Student deleted successfully!")
elif choice == "3":
name = input("Enter name: ")
email = input("Enter email: ")
phone = input("Enter phone: ")
contact_list.modify_student(name, email, phone)
print("Student modified successfully!")
elif choice == "4":
name = input("Enter name: ")
student = contact_list.get_student(name)
print(f"Name: {student[0]}, Email: {student[1]}, Phone: {student[2]}")
elif choice == "5":
contact_list.get_all_students()
elif choice == "6":
print("Exiting...")
break
else:
print("Invalid choice")
if __name__ == '__main__':
main()
```
这段代码中,我们定义了一个学生类 Student,包含学生的姓名、邮箱和电话三个属性;然后定义了一个通讯录类 ContactList,包含了添加学生、删除学生、修改学生、查询学生、获取所有学生通讯信息及退出系统的功能。最后在 main 函数中调用这些功能实现了一个简单的学生通讯录管理系统。