- Python基础
- Python - 首页
- Python - 概述
- Python - 历史
- Python - 特性
- Python vs C++
- Python - Hello World程序
- Python - 应用领域
- Python - 解释器
- Python - 环境搭建
- Python - 虚拟环境
- Python - 基本语法
- Python - 变量
- Python - 数据类型
- Python - 类型转换
- Python - Unicode系统
- Python - 字面量
- Python - 运算符
- Python - 算术运算符
- Python - 比较运算符
- Python - 赋值运算符
- Python - 逻辑运算符
- Python - 位运算符
- Python - 成员运算符
- Python - 身份运算符
- Python - 运算符优先级
- Python - 注释
- Python - 用户输入
- Python - 数字
- Python - 布尔值
- Python控制语句
- Python - 控制流
- Python - 决策
- Python - If语句
- Python - If else
- Python - 嵌套If
- Python - Match-Case语句
- Python - 循环
- Python - for循环
- Python - for-else循环
- Python - while循环
- Python - break语句
- Python - continue语句
- Python - pass语句
- Python - 嵌套循环
- Python函数与模块
- Python - 函数
- Python - 默认参数
- Python - 关键字参数
- Python - 仅关键字参数
- Python - 位置参数
- Python - 仅位置参数
- Python - 可变参数
- Python - 变量作用域
- Python - 函数注解
- Python - 模块
- Python - 内置函数
- Python字符串
- Python - 字符串
- Python - 字符串切片
- Python - 修改字符串
- Python - 字符串连接
- Python - 字符串格式化
- Python - 转义字符
- Python - 字符串方法
- Python - 字符串练习
- Python列表
- Python - 列表
- Python - 访问列表元素
- Python - 修改列表元素
- Python - 添加列表元素
- Python - 删除列表元素
- Python - 遍历列表
- Python - 列表推导式
- Python - 排序列表
- Python - 复制列表
- Python - 合并列表
- Python - 列表方法
- Python - 列表练习
- Python元组
- Python - 元组
- Python - 访问元组元素
- Python - 更新元组
- Python - 解包元组
- Python - 遍历元组
- Python - 合并元组
- Python - 元组方法
- Python - 元组练习
- Python集合
- Python - 集合
- Python - 访问集合元素
- Python - 添加集合元素
- Python - 删除集合元素
- Python - 遍历集合
- Python - 合并集合
- Python - 复制集合
- Python - 集合运算符
- Python - 集合方法
- Python - 集合练习
- Python字典
- Python - 字典
- Python - 访问字典元素
- Python - 修改字典元素
- Python - 添加字典元素
- Python - 删除字典元素
- Python - 字典视图对象
- Python - 遍历字典
- Python - 复制字典
- Python - 嵌套字典
- Python - 字典方法
- Python - 字典练习
- Python数组
- Python - 数组
- Python - 访问数组元素
- Python - 添加数组元素
- Python - 删除数组元素
- Python - 遍历数组
- Python - 复制数组
- Python - 反转数组
- Python - 排序数组
- Python - 合并数组
- Python - 数组方法
- Python - 数组练习
- Python文件处理
- Python - 文件处理
- Python - 写入文件
- Python - 读取文件
- Python - 重命名和删除文件
- Python - 目录
- Python - 文件方法
- Python - OS文件/目录方法
- Python - OS路径方法
- 面向对象编程
- Python - OOPs概念
- Python - 类与对象
- Python - 类属性
- Python - 类方法
- Python - 静态方法
- Python - 构造函数
- Python - 访问修饰符
- Python - 继承
- Python - 多态
- Python - 方法重写
- Python - 方法重载
- Python - 动态绑定
- Python - 动态类型
- Python - 抽象
- Python - 封装
- Python - 接口
- Python - 包
- Python - 内部类
- Python - 匿名类和对象
- Python - 单例类
- Python - 包装类
- Python - 枚举
- Python - 反射
- Python错误与异常
- Python - 语法错误
- Python - 异常
- Python - try-except块
- Python - try-finally块
- Python - 抛出异常
- Python - 异常链
- Python - 嵌套try块
- Python - 用户自定义异常
- Python - 日志记录
- Python - 断言
- Python - 内置异常
- Python多线程
- Python - 多线程
- Python - 线程生命周期
- Python - 创建线程
- Python - 启动线程
- Python - 加入线程
- Python - 线程命名
- Python - 线程调度
- Python - 线程池
- Python - 主线程
- Python - 线程优先级
- Python - 守护线程
- Python - 线程同步
- Python同步
- Python - 线程间通信
- Python - 线程死锁
- Python - 中断线程
- Python网络编程
- Python - 网络编程
- Python - Socket编程
- Python - URL处理
- Python - 泛型
- Python库
- NumPy教程
- Pandas教程
- SciPy教程
- Matplotlib教程
- Django教程
- OpenCV教程
- Python杂项
- Python - 日期与时间
- Python - 数学
- Python - 迭代器
- Python - 生成器
- Python - 闭包
- Python - 装饰器
- Python - 递归
- Python - 正则表达式
- Python - PIP
- Python - 数据库访问
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 输出格式化
- Python - 性能测量
- Python - 数据压缩
- Python - CGI编程
- Python - XML处理
- Python - GUI编程
- Python - 命令行参数
- Python - 文档字符串
- Python - JSON
- Python - 发送邮件
- Python - 扩展
- Python - 工具/实用程序
- Python - GUIs
- Python高级概念
- Python - 抽象基类
- Python - 自定义异常
- Python - 高阶函数
- Python - 对象内部
- Python - 内存管理
- Python - 元类
- Python - 使用元类进行元编程
- Python - 模拟和存根
- Python - 猴子补丁
- Python - 信号处理
- Python - 类型提示
- Python - 自动化教程
- Python - Humanize包
- Python - 上下文管理器
- Python - 协程
- Python - 描述符
- Python - 诊断和修复内存泄漏
- Python - 不可变数据结构
- Python实用资源
- Python - 问答
- Python - 在线测验
- Python - 快速指南
- Python - 参考
- Python - 速查表
- Python - 项目
- Python - 实用资源
- Python - 讨论
- Python编译器
- NumPy编译器
- Matplotlib编译器
- SciPy编译器
Python通讯录应用程序
Python通讯录应用程序是一个简单但有效的工具,用于管理个人联系人。通过此应用程序,用户可以添加、查看、删除和搜索联系人,同时确保电话号码得到正确验证。通讯录将所有信息存储在JSON文件中,以便轻松地在会话之间保留数据。
通讯录应用程序的工作流程
通讯录应用程序旨在使用Python管理您的联系人。以下是它的工作原理:
- 加载联系人 应用程序启动时,会检查是否存在contact.json文件。如果找到,则将保存的联系人加载到程序中。
- 添加联系人 您可以通过提供姓名、电话号码和电子邮件来添加新的联系人。电话号码将被检查以确保它是数字,并且不超过10位。
- 查看联系人 要查看所有联系人,只需选择查看选项。应用程序将以用户友好的格式显示每个联系人的详细信息。
- 删除联系人 如果您需要删除联系人,只需输入联系人的姓名,如果存在,应用程序将将其从列表中删除。
- 搜索联系人 您可以按姓名快速找到联系人。如果联系人存在于列表中,应用程序将显示详细信息。
- 保存联系人 当您退出应用程序时,您的更改将保存到contacts.json中,因此您不会丢失任何信息。
实现通讯录应用程序的Python代码
import json
import os
import re
# File to store contacts
CONTACTS_FILE = 'contacts.json'
def load_contacts():
"""Load contacts from the JSON file."""
if os.path.exists(CONTACTS_FILE):
with open(CONTACTS_FILE, 'r') as file:
return json.load(file)
return {}
def save_contacts(contacts):
"""Save contacts to the JSON file."""
with open(CONTACTS_FILE, 'w') as file:
json.dump(contacts, file, indent=4)
def add_contact(contacts):
"""Add a new contact with phone number validation."""
name = input("Enter contact name: ").strip()
phone = input("Enter contact phone number: ").strip()
# Validate phone number length
if len(phone) > 10:
print("Phone number should not exceed 10 digits.")
return
# Optional: Further validate phone number format (e.g., only digits)
if not re.match(r'^\d+$', phone):
print("Phone number should contain only digits.")
return
email = input("Enter contact email: ").strip()
contacts[name] = {'phone': phone, 'email': email}
print(f"Contact '{name}' added.")
def view_contacts(contacts):
"""View all contacts."""
if not contacts:
print("No contacts found.")
return
for name, info in contacts.items():
print(f"Name: {name}")
print(f"Phone: {info['phone']}")
print(f"Email: {info['email']}\n")
def delete_contact(contacts):
"""Delete a contact."""
name = input("Enter contact name to delete: ").strip()
if name in contacts:
del contacts[name]
print(f"Contact '{name}' deleted.")
else:
print("Contact not found.")
def search_contact(contacts):
"""Search for a contact."""
name = input("Enter contact name to search: ").strip()
if name in contacts:
info = contacts[name]
print(f"Name: {name}")
print(f"Phone: {info['phone']}")
print(f"Email: {info['email']}")
else:
print("Contact not found.")
def main():
contacts = load_contacts()
while True:
print("\nContact Book")
print("1. Add Contact")
print("2. View Contacts")
print("3. Delete Contact")
print("4. Search Contact")
print("5. Exit")
choice = input("Enter your choice: ").strip()
if choice == '1':
add_contact(contacts)
elif choice == '2':
view_contacts(contacts)
elif choice == '3':
delete_contact(contacts)
elif choice == '4':
search_contact(contacts)
elif choice == '5':
save_contacts(contacts)
print("Contacts saved. Exiting...")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
输出
Contact Book 1. Add Contact 2. View Contacts 3. Delete Contact 4. Search Contact 5. Exit Enter your choice: 1 Enter contact name: Sudhir Enter contact phone number: 1234567890 Enter contact email: temp1@gmail.com Contact 'Sudhir' added. Contact Book 1. Add Contact 2. View Contacts 3. Delete Contact 4. Search Contact 5. Exit Enter your choice: 2 1 Enter contact name: Krishna Enter contact phone number: 1118882340 Enter contact email: kkk@gmail, .com Contact 'Krishna' added. Contact Book 1. Add Contact 2. View Contacts 3. Delete Contact 4. Search Contact 5. Exit Enter your choice: 2 Name: Sudhir Phone: 1234567890 Email: temp1@gmail.com Name: Krishna Phone: 1118882340 Email: kkk@gmail.com Contact Book 1. Add Contact 2. View Contacts 3. Delete Contact 4. Search Contact 5. Exit Enter your choice: 5 Contacts saved. Exiting...
这是正常的输出,但是如果您在电话号码中输入超过10个数字,它将显示:
Contact Book 1. Add Contact 2. View Contacts 3. Delete Contact 4. Search Contact 5. Exit Enter your choice: 1 Enter contact name: Sudhir Enter contact phone number: 777 123456789012 Phone number should not exceed 10 digits. Contact Book 1. Add Contact 2. View Contacts 3. Delete Contact 4. Search Contact 5. Exit Enter your choice: 5 Contacts saved. Exiting...
python_projects_from_basic_to_advanced.htm
广告