以下是一个Python小程序的示例,它实现了一个简单的联系人管理器。用户可以添加、查看、编辑和删除联系人信息
代码
import json
# 联系人列表文件路径
CONTACTS_FILE = 'contacts.json'
# 保存联系人列表
def save_contacts(contacts):
with open(CONTACTS_FILE, 'w') as file:
json.dump(contacts, file, indent=4)
# 加载联系人列表
def load_contacts():
try:
with open(CONTACTS_FILE, 'r') as file:
return json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
return {}
# 添加联系人
def add_contact():
contacts = load_contacts()
name = input("Enter the contact's name: ")
phone = input("Enter the contact's phone number: ")
email = input("Enter the contact's email (optional): ")
contacts[name] = {'Phone': phone, 'Email': email}
save_contacts(contacts)
print(f"Contact {name} added successfully.")
# 显示所有联系人
def list_contacts():
contacts = load_contacts()
if contacts:
print("Contacts List:")
for name, info in contacts.items():
print(f"Name: {name}, Phone: {info['Phone']}, Email: {info['Email']}")
else:
print("No contacts found.")
# 更新联系人
def update_contact():
contacts = load_contacts()
name = input("Enter the name of the contact to update: ")
if name in contacts:
phone = input("Enter the new phone number (leave blank to keep current): ")
email = input("Enter the new email (leave blank to keep current): ")
if phone:
contacts[name]['Phone'] = phone
if email:
contacts[name]['Email'] = email
save_contacts(contacts)
print(f"Contact {name} updated successfully.")
else:
print("Contact not found.")
# 删除联系人
def delete_contact():
contacts = load_contacts()
name = input("Enter the name of the contact to delete: ")
if name in contacts:
del contacts[name]
save_contacts(contacts)
print(f"Contact {name} deleted successfully.")
else:
print("Contact not found.")
# 主程序
def main():
while True:
print("\nContact Manager App")
print("1. Add Contact")
print("2. List Contacts")
print("3. Update Contact")
print("4. Delete Contact")
print("5. Exit")
choice = input("Enter your choice (1-5): ")
if choice == '1':
add_contact()
elif choice == '2':
list_contacts()
elif choice == '3':
update_contact()
elif choice == '4':
delete_contact()
elif choice == '5':
print("Exiting the app.")
break
else:
print("Invalid choice, please enter a number between 1 and 5.")
if __name__ == "__main__":
main()
可以直接复制代码保存为.py
import json
# 联系人列表文件路径
CONTACTS_FILE = 'contacts.json'
# 保存联系人列表
def save_contacts(contacts):
with open(CONTACTS_FILE, 'w') as file:
json.dump(contacts, file, indent=4)
# 加载联系人列表
def load_contacts():
try:
with open(CONTACTS_FILE, 'r') as file:
return json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
return {}
# 添加联系人
def add_contact():
contacts = load_contacts()
name = input("Enter the contact's name: ")
phone = input("Enter the contact's phone number: ")
email = input("Enter the contact's email (optional): ")
contacts[name] = {'Phone': phone, 'Email': email}
save_contacts(contacts)
print(f"Contact {name} added successfully.")
# 显示所有联系人
def list_contacts():
contacts = load_contacts()
if contacts:
print("Contacts List:")
for name, info in contacts.items():
print(f"Name: {name}, Phone: {info['Phone']}, Email: {info['Email']}")
else:
print("No contacts found.")
# 更新联系人
def update_contact():
contacts = load_contacts()
name = input("Enter the name of the contact to update: ")
if name in contacts:
phone = input("Enter the new phone number (leave blank to keep current): ")
email = input("Enter the new email (leave blank to keep current): ")
if phone:
contacts[name]['Phone'] = phone
if email:
contacts[name]['Email'] = email
save_contacts(contacts)
print(f"Contact {name} updated successfully.")
else:
print("Contact not found.")
# 删除联系人
def delete_contact():
contacts = load_contacts()
name = input("Enter the name of the contact to delete: ")
if name in contacts:
del contacts[name]
save_contacts(contacts)
print(f"Contact {name} deleted successfully.")
else:
print("Contact not found.")
# 主程序
def main():
while True:
print("\nContact Manager App")
print("1. Add Contact")
print("2. List Contacts")
print("3. Update Contact")
print("4. Delete Contact")
print("5. Exit")
choice = input("Enter your choice (1-5): ")
if choice == '1':
add_contact()
elif choice == '2':
list_contacts()
elif choice == '3':
update_contact()
elif choice == '4':
delete_contact()
elif choice == '5':
print("Exiting the app.")
break
else:
print("Invalid choice, please enter a number between 1 and 5.")
if __name__ == "__main__":
main()
这个程序使用JSON文件来存储联系人信息,允许用户通过命令行界面进行操作。用户可以添加新的联系人、列出所有联系人、更新或删除现有联系人。
保存为123.py。然后在python环境中运行他。
python 123.py
运行效果
依次是 添加 列出 更新删除 退出选项。是不是很简单哦 感兴趣的朋友可以测试下哦