-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContact_list.py
More file actions
96 lines (96 loc) · 2.94 KB
/
Copy pathContact_list.py
File metadata and controls
96 lines (96 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "adfbd5a8-7a45-4922-8e60-4e3a3af9f401",
"metadata": {},
"outputs": [],
"source": [
"contact_list = []\n",
"\n",
"file_name = \"contact_list.txt\"\n",
"\n",
"def add_contact(contact):\n",
" contact_list.append(contact)\n",
" print(\"Contact added successfully!\")\n",
"\n",
"def remove_contact(index):\n",
" if index < len(contact_list):\n",
" del contact_list[index]\n",
" print(\"Contact removed successfully!\")\n",
" else:\n",
" print(\"Invalid contact index\")\n",
"\n",
"def view_contacts():\n",
" if len(contact_list) == 0:\n",
" print(\"No contacts in the list\")\n",
" else:\n",
" for i, contact in enumerate(contact_list):\n",
" print(f\"{i+1}. {contact}\")\n",
"\n",
"def search_contacts(search):\n",
" results = [contact for contact in contact_list if search.lower() in contact.lower()]\n",
" if len(results) == 0:\n",
" print(\"No contacts found\")\n",
" else:\n",
" for i, contact in enumerate(results):\n",
" print(f\"{i+1}. {contact}\")\n",
"\n",
"def save_contacts():\n",
" with open(file_name, 'w') as file:\n",
" for contact in contact_list:\n",
" file.write(contact + '\\n')\n",
" print(\"Contacts saved to file!\")\n",
"\n",
"while True:\n",
" print(\"\\n===== Contact List =====\")\n",
" print(\"1. Add a Contact\")\n",
" print(\"2. Remove a Contact\")\n",
" print(\"3. View Contacts\")\n",
" print(\"4. Search Contacts\")\n",
" print(\"5. Save Contacts to file\")\n",
"\n",
" choice = input(\"Enter your choice: \")\n",
"\n",
" if choice == \"1\":\n",
" contact = input(\"Enter the contact to add: \")\n",
" add_contact(contact)\n",
" elif choice == \"2\":\n",
" index = int(input(\"Enter the index of the Contact to remove: \")) - 1\n",
" remove_contact(index)\n",
" elif choice == \"3\":\n",
" view_contacts()\n",
" elif choice == \"4\":\n",
" search = input(\"Enter the name of the contact to search: \")\n",
" search_contacts(search)\n",
" elif choice == \"5\":\n",
" save_contacts()\n",
" break\n",
" else:\n",
" print(\"Invalid choice. Please try again.\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}