手机通讯录python编写
时间: 2024-10-15 20:07:46 浏览: 103
手机通讯录功能通常是一个应用级别的功能,利用Python可以模拟一个简单的联系人管理程序。Python通过内置的数据结构如列表、字典等来存储联系人的信息,比如姓名、电话号码、电子邮件等。下面是一个简化的例子:
```python
class Contact:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
# 创建一个通讯录
contact_book = {}
def add_contact(name, phone, email):
contact_id = len(contact_book) + 1
new_contact = Contact(name, phone, email)
contact_book[contact_id] = new_contact
print(f"Contact {name} added with ID {contact_id}")
def view_contact(id):
if id in contact_book:
contact = contact_book[id]
print(f"{contact.name}, Phone: {contact.phone}, Email: {contact.email}")
else:
print("Contact not found.")
def main():
while True:
action = input("Enter 'add', 'view', or 'quit': ")
if action == "quit":
break
elif action == "add":
name = input("Enter name: ")
phone = input("Enter phone number: ")
email = input("Enter email: ")
add_contact(name, phone, email)
elif action == "view":
id = int(input("Enter contact ID: "))
view_contact(id)
if __name__ == "__main__":
main()
```
在这个示例中,用户可以添加新的联系人、查看特定联系人或退出程序。
阅读全文