手机通讯录 程序设计
时间: 2023-08-17 15:05:59 浏览: 43
这里给出一个简单的手机通讯录程序设计:
1. 首先,我们需要定义联系人的信息。每个联系人应该包含以下信息:姓名、电话号码、地址、电子邮件地址等。我们可以使用一个类来表示联系人信息。
```
class Contact:
def __init__(self, name, phone_number, address='', email=''):
self.name = name
self.phone_number = phone_number
self.address = address
self.email = email
```
2. 接下来,我们需要创建一个通讯录类来管理联系人。我们需要实现以下功能:
- 添加联系人
- 删除联系人
- 查找联系人
- 显示所有联系人
```
class AddressBook:
def __init__(self):
self.contacts = []
def add_contact(self, contact):
self.contacts.append(contact)
def remove_contact(self, contact):
self.contacts.remove(contact)
def find_contact(self, name):
for contact in self.contacts:
if contact.name == name:
return contact
return None
def display_contacts(self):
for contact in self.contacts:
print(f'Name: {contact.name}')
print(f'Phone Number: {contact.phone_number}')
print(f'Address: {contact.address}')
print(f'Email: {contact.email}')
print('---------------------')
```
3. 最后,我们可以使用一个简单的命令行界面来与用户交互。用户可以选择添加、删除、查找或显示所有联系人。
```
if __name__ == '__main__':
address_book = AddressBook()
while True:
print('1. Add Contact')
print('2. Remove Contact')
print('3. Find Contact')
print('4. Display All Contacts')
print('5. Exit')
choice = input('Enter your choice: ')
if choice == '1':
name = input('Enter name: ')
phone_number = input('Enter phone number: ')
address = input('Enter address: ')
email = input('Enter email: ')
contact = Contact(name, phone_number, address, email)
address_book.add_contact(contact)
print('Contact added successfully!')
elif choice == '2':
name = input('Enter name: ')
contact = address_book.find_contact(name)
if contact:
address_book.remove_contact(contact)
print('Contact removed successfully!')
else:
print('Contact not found!')
elif choice == '3':
name = input('Enter name: ')
contact = address_book.find_contact(name)
if contact:
print(f'Name: {contact.name}')
print(f'Phone Number: {contact.phone_number}')
print(f'Address: {contact.address}')
print(f'Email: {contact.email}')
else:
print('Contact not found!')
elif choice == '4':
address_book.display_contacts()
elif choice == '5':
break
else:
print('Invalid choice!')
```
这是一个简单的手机通讯录程序设计,你可以根据需要自行扩展它的功能。