用python写一个电话管理系统
时间: 2023-07-18 07:10:11 浏览: 178
python实现电话簿管理系统.zip
5星 · 资源好评率100%
好的,我可以为您提供一个简单的电话管理系统的Python代码,让您参考:
```python
class Contact:
def __init__(self, name, phone_number):
self.name = name
self.phone_number = phone_number
class Phonebook:
def __init__(self):
self.contacts = []
def add_contact(self, name, phone_number):
contact = Contact(name, phone_number)
self.contacts.append(contact)
def search_contact(self, name):
for contact in self.contacts:
if contact.name == name:
return contact.phone_number
return "Contact not found."
def remove_contact(self, name):
for contact in self.contacts:
if contact.name == name:
self.contacts.remove(contact)
return "Contact removed."
return "Contact not found."
phonebook = Phonebook()
while True:
print("1. Add contact")
print("2. Search contact")
print("3. Remove contact")
print("4. Quit")
choice = input("Enter your choice: ")
if choice == "1":
name = input("Enter the name: ")
phone_number = input("Enter the phone number: ")
phonebook.add_contact(name, phone_number)
print("Contact added.")
elif choice == "2":
name = input("Enter the name: ")
phone_number = phonebook.search_contact(name)
print(phone_number)
elif choice == "3":
name = input("Enter the name: ")
message = phonebook.remove_contact(name)
print(message)
elif choice == "4":
break
else:
print("Invalid choice. Please try again.")
```
这个电话管理系统使用了Python类和对象的概念,包括联系人(Contact)和电话簿(Phonebook)两个类。您可以通过选择不同的选项来添加联系人,搜索联系人或删除联系人。
阅读全文