python写学生通讯录管理系统
时间: 2023-07-03 16:27:49 浏览: 181
好的,我可以为你提供一个简单的 Python 学生通讯录管理系统的实现思路,包括以下功能:添加学生信息、删除学生信息、查找学生信息、修改学生信息、显示学生信息、退出系统。具体实现如下:
1. 搭建基本框架
首先,我们需要创建一个学生通讯录类,包含各种操作学生信息的方法,如添加、删除、查找、修改、显示等。同时,我们需要在主程序中创建一个菜单,使用户可以选择需要进行的操作。
```python
class Student:
def __init__(self, name, age, gender, phone):
self.name = name
self.age = age
self.gender = gender
self.phone = phone
def get_info(self):
return f"{self.name}, {self.age}, {self.gender}, {self.phone}"
class StudentList:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, name):
for s in self.students:
if s.name == name:
self.students.remove(s)
def find_student(self, name):
for s in self.students:
if s.name == name:
return s
return None
def update_student(self, name, age, gender, phone):
for s in self.students:
if s.name == name:
s.age = age
s.gender = gender
s.phone = phone
def show_students(self):
for s in self.students:
print(s.get_info())
def show_menu():
print("1. 添加学生信息")
print("2. 删除学生信息")
print("3. 查找学生信息")
print("4. 修改学生信息")
print("5. 显示学生信息")
print("6. 退出系统")
```
2. 实现添加学生信息功能
```python
def add_student_info():
name = input("请输入学生姓名:")
age = input("请输入学生年龄:")
gender = input("请输入学生性别:")
phone = input("请输入学生电话:")
student = Student(name, age, gender, phone)
student_list.add_student(student)
print("添加学生信息成功!")
```
3. 实现删除学生信息功能
```python
def remove_student_info():
name = input("请输入学生姓名:")
student_list.remove_student(name)
print("删除学生信息成功!")
```
4. 实现查找学生信息功能
```python
def find_student_info():
name = input("请输入学生姓名:")
student = student_list.find_student(name)
if student:
print(student.get_info())
else:
print("未找到该学生信息!")
```
5. 实现修改学生信息功能
```python
def update_student_info():
name = input("请输入学生姓名:")
age = input("请输入学生年龄:")
gender = input("请输入学生性别:")
phone = input("请输入学生电话:")
student_list.update_student(name, age, gender, phone)
print("修改学生信息成功!")
```
6. 实现显示学生信息功能
```python
def show_student_info():
student_list.show_students()
```
7. 实现退出系统功能
```python
def exit_system():
print("再见!")
sys.exit(0)
```
8. 完整代码
```python
import sys
class Student:
def __init__(self, name, age, gender, phone):
self.name = name
self.age = age
self.gender = gender
self.phone = phone
def get_info(self):
return f"{self.name}, {self.age}, {self.gender}, {self.phone}"
class StudentList:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, name):
for s in self.students:
if s.name == name:
self.students.remove(s)
def find_student(self, name):
for s in self.students:
if s.name == name:
return s
return None
def update_student(self, name, age, gender, phone):
for s in self.students:
if s.name == name:
s.age = age
s.gender = gender
s.phone = phone
def show_students(self):
for s in self.students:
print(s.get_info())
def show_menu():
print("1. 添加学生信息")
print("2. 删除学生信息")
print("3. 查找学生信息")
print("4. 修改学生信息")
print("5. 显示学生信息")
print("6. 退出系统")
def add_student_info():
name = input("请输入学生姓名:")
age = input("请输入学生年龄:")
gender = input("请输入学生性别:")
phone = input("请输入学生电话:")
student = Student(name, age, gender, phone)
student_list.add_student(student)
print("添加学生信息成功!")
def remove_student_info():
name = input("请输入学生姓名:")
student_list.remove_student(name)
print("删除学生信息成功!")
def find_student_info():
name = input("请输入学生姓名:")
student = student_list.find_student(name)
if student:
print(student.get_info())
else:
print("未找到该学生信息!")
def update_student_info():
name = input("请输入学生姓名:")
age = input("请输入学生年龄:")
gender = input("请输入学生性别:")
phone = input("请输入学生电话:")
student_list.update_student(name, age, gender, phone)
print("修改学生信息成功!")
def show_student_info():
student_list.show_students()
def exit_system():
print("再见!")
sys.exit(0)
if __name__ == "__main__":
student_list = StudentList()
while True:
show_menu()
choice = input("请选择要进行的操作:")
if choice == "1":
add_student_info()
elif choice == "2":
remove_student_info()
elif choice == "3":
find_student_info()
elif choice == "4":
update_student_info()
elif choice == "5":
show_student_info()
elif choice == "6":
exit_system()
else:
print("输入有误,请重新输入!")
```
以上就是一个简单的 Python 学生通讯录管理系统的实现思路,你可以根据需要进行修改和完善。
阅读全文