用python写一个电话管理系统,包括1,添加联系人,2,删除联系人,3,搜索联系人4,编辑联系人,5,显示联系人
时间: 2023-10-11 13:11:32 浏览: 120
C++基于qt框架开发的一款手机通讯录管理系统源码,可以实现增加、删除、编辑、搜索联系人的功能
5星 · 资源好评率100%
好的,以下是一个包含添加、删除、搜索、编辑和显示联系人的电话管理系统Python代码:
```python
class Contact:
def __init__(self, name, phone_number, email):
self.name = name
self.phone_number = phone_number
self.email = email
class Phonebook:
def __init__(self):
self.contacts = []
def add_contact(self, name, phone_number, email):
contact = Contact(name, phone_number, email)
self.contacts.append(contact)
print("Contact added.")
def search_contact(self, name):
for contact in self.contacts:
if contact.name == name:
print(f"Name: {contact.name} Phone number: {contact.phone_number} Email: {contact.email}")
return
print("Contact not found.")
def remove_contact(self, name):
for contact in self.contacts:
if contact.name == name:
self.contacts.remove(contact)
print("Contact removed.")
return
print("Contact not found.")
def edit_contact(self, name):
for contact in self.contacts:
if contact.name == name:
new_name = input("Enter the new name (press enter to skip): ")
new_phone_number = input("Enter the new phone number (press enter to skip): ")
new_email = input("Enter the new email (press enter to skip): ")
if new_name:
contact.name = new_name
if new_phone_number:
contact.phone_number = new_phone_number
if new_email:
contact.email = new_email
print("Contact edited.")
return
print("Contact not found.")
def display_contacts(self):
if self.contacts:
for contact in self.contacts:
print(f"Name: {contact.name} Phone number: {contact.phone_number} Email: {contact.email}")
else:
print("No contacts found.")
phonebook = Phonebook()
while True:
print("1. Add contact")
print("2. Search contact")
print("3. Remove contact")
print("4. Edit contact")
print("5. Display contacts")
print("6. Quit")
choice = input("Enter your choice: ")
if choice == "1":
name = input("Enter the name: ")
phone_number = input("Enter the phone number: ")
email = input("Enter the email: ")
phonebook.add_contact(name, phone_number, email)
elif choice == "2":
name = input("Enter the name: ")
phonebook.search_contact(name)
elif choice == "3":
name = input("Enter the name: ")
phonebook.remove_contact(name)
elif choice == "4":
name = input("Enter the name: ")
phonebook.edit_contact(name)
elif choice == "5":
phonebook.display_contacts()
elif choice == "6":
break
else:
print("Invalid choice. Please try again.")
```
这个电话管理系统使用了Python类和对象的概念,包括联系人(Contact)和电话簿(Phonebook)两个类。您可以通过选择不同的选项来添加、删除、搜索、编辑和显示联系人。
阅读全文