-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCONTACTS-PROGRAM
More file actions
208 lines (109 loc) · 4.22 KB
/
CONTACTS-PROGRAM
File metadata and controls
208 lines (109 loc) · 4.22 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
FILENAME="CONTACTS.TXT"
def add_contacts():
try:
name=input("Enter Name: ")
# Keep asking until user enters a valid 10-digit number
while True:
phone = input("Enter Phone Number: ")
if phone.isdigit() and len(phone) == 10:
break
else:
print("Invalid phone number. Please enter exactly 10 digits.")
with open("contacts.txt","a") as file:
file.write(f"{name},{phone}\n")
print("Contact added successfully.")
except Exception as e:
print(f"an error occureed: {e}")
def view_contacts():
try:
with open(FILENAME,"r") as f:
contacts=f.readlines()
if not contacts:
print("no contacts found ")
else:
print("\nContacts List:")
for i, contact in enumerate(contacts, start=1):
parts = contact.strip().split(",")
if len(parts) == 2: # only process valid lines
name, phone = parts
print(f"{i}. {name} -- {phone}")
else:
print(f"{i}. [Invalid entry: {contact.strip()}]")
except FileNotFoundError:
print("no contacts found, please add a contact first ")
except Exception as e:
print("error while reading a file ",e)
def update_contact():
try:
view_contacts()
choice=int(input("Enter the contact number to update: "))
with open (FILENAME,"r") as f:
contacts=f.readlines()
if 1<=choice<=len(contacts):
name,phone=contacts[choice-1].strip().split(",")
print(f"Current Name: {name}, Current Phone: {phone}")
new_name=input("Enter new name (leave blank to keep current: ") or name
while True:
new_phone = input("Enter new phone number (leave blank to keep current): ") or phone
if new_phone.isdigit() and len(new_phone) == 10:
break
elif new_phone == phone: # user kept old value
break
else:
print("Invalid phone number. Please enter exactly 10 digits.")
contacts[choice-1]=f"{new_name},{new_phone}\n"
with open(FILENAME,"w") as f:
f.writelines(contacts)
print("Contact updated successfully.")
else:
print("Invalid contact number.")
except ValueError:
print("Please enter a valid number.")
except Exception as e:
print(f"an error occurred",e)
def delete_contact():
try:
view_contacts()
choice=int(input("Enter the contact number to delete: "))
with open(FILENAME,"r") as f:
contacts=f.readlines()
if 1<=choice<=len(contacts):
deleted=contacts.pop(choice-1)
with open(FILENAME,"w") as f:
f.writelines(contacts)
print(f"Contact {deleted.strip()} deleted successfully.")
else:
print("Invalid contact number.")
except ValueError:
print("Please enter a valid number.")
except Exception as e:
print(f"an error occurred:{e}")
def main():
while True:
print("\nContact Management System")
print("1. Add Contact")
print("2.View Contacts")
print("3.Update Contact")
print("4.Delete Contacts")
print("5.exit")
try:
choice=int(input("Enter your choice (1-5): "))
if choice==1:
add_contacts()
elif choice==2:
view_contacts()
elif choice==3:
update_contact()
elif choice==4:
delete_contact()
elif choice==5:
print("Exiting the program.")
break
else:
print("invalid choice")
except ValueError:
print("Please enter a valid number.")
except Exception as e:
print(f"an error occurred: {e}")
if __name__=="__main__":
main()