
- 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
廣告