117.info
人生若只如初见

python实现简单通讯录管理系统

下面是一个简单的通讯录管理系统的Python实现:

class Contact:
def __init__(self, name, phone):
self.name = name
self.phone = phone
class ContactBook:
def __init__(self):
self.contacts = []
def add_contact(self, name, phone):
contact = Contact(name, phone)
self.contacts.append(contact)
print("Contact added successfully.")
def delete_contact(self, name):
for contact in self.contacts:
if contact.name == name:
self.contacts.remove(contact)
print("Contact deleted successfully.")
return
print("Contact not found.")
def search_contact(self, name):
for contact in self.contacts:
if contact.name == name:
print("Contact found - Name: {}, Phone: {}".format(contact.name, contact.phone))
return
print("Contact not found.")
def display_contacts(self):
if len(self.contacts) == 0:
print("No contacts found.")
else:
print("Contacts:")
for contact in self.contacts:
print("Name: {}, Phone: {}".format(contact.name, contact.phone))
def menu():
print("1. Add Contact")
print("2. Delete Contact")
print("3. Search Contact")
print("4. Display Contacts")
print("5. Quit")
contact_book = ContactBook()
while True:
menu()
choice = int(input("Enter your choice: "))
if choice == 1:
name = input("Enter name: ")
phone = input("Enter phone number: ")
contact_book.add_contact(name, phone)
elif choice == 2:
name = input("Enter name: ")
contact_book.delete_contact(name)
elif choice == 3:
name = input("Enter name: ")
contact_book.search_contact(name)
elif choice == 4:
contact_book.display_contacts()
elif choice == 5:
break
else:
print("Invalid choice. Please try again.")

该程序使用了两个类:Contact表示一个联系人,ContactBook表示通讯录。ContactBook类包含了添加联系人、删除联系人、搜索联系人和显示联系人等方法。主程序循环显示菜单,根据用户选择执行相应的操作。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe77bAzsLBgFWBFQ.html

推荐文章

  • linux的python如何进行进程管理

    在Linux中,可以使用Python的内置库os和subprocess进行进程管理 使用os.system()执行外部命令: import os # 执行外部命令,例如:ls
    os.system("ls") 使用...

  • linux的python怎样进行系统调用

    在Linux中,Python可以通过os和sys库进行系统调用 使用os库: os库提供了许多与操作系统交互的函数。例如,要使用os.system()执行系统命令,可以这样做:
    i...

  • python中set怎样进行集合排序

    在Python中,可以使用sorted()函数对集合(set)进行排序。sorted()函数返回一个排序后的列表(list),而不是集合,因为集合本身是无序的。以下是一个示例:

  • python中set如何进行集合转换

    在Python中,可以使用集合(set)的内置方法进行集合转换。以下是一些常用的集合转换方法: 将列表转换为集合: my_list = [1, 2, 3, 4, 5]
    my_set = set(m...

  • response对象的使用(实例讲解)

    response对象是HTTP请求的响应结果,它包含了服务器返回的数据以及与响应相关的信息,如状态码、头部信息等。在实际使用中,我们可以通过response对象来获取和处...

  • Oracle to_char函数的使用方法

    Oracle中的TO_CHAR函数用于将数据转换为字符串格式。TO_CHAR函数有多个参数,以下是常用的使用方法: 将日期转换为字符串: TO_CHAR(date, ‘format’)
    示例...

  • shell产生随机数七种方法的实现

    在shell脚本中,有多种方法可以生成随机数。以下是七种常见的方法: 使用$RANDOM变量:$RANDOM是shell内置的随机数变量,它会在每次访问时生成一个0到32767之间的...

  • jquery实现全选、反选、获得所有选中的checkbox

    要使用jQuery实现全选、反选和获取所有选中的复选框,可以按照以下步骤进行操作。首先,确保在页面中引入了jQuery库文件。1、全选功能:为了实现全选功能,您需要...