-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniProject_contactManager.py
More file actions
74 lines (64 loc) · 2.06 KB
/
MiniProject_contactManager.py
File metadata and controls
74 lines (64 loc) · 2.06 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
# Create a simple contact manager that:
# - Stores name and phone number
# - Allows adding new contacts
# - Shows all contacts
# - Use lists or dictionary (your choice)
import json
def add_person():
name=input("enter the name: ")
phonenumber=int(input("enter the number: "))
person={ "name":name,
"mobilenumber":phonenumber
}
return person
def get_people(people):
for i,person in enumerate(people):
print(i+1 , '-',person['name'],"|",person['mobilenumber'])
def delete(people):
get_people(people)
while True:
number=input("enter number: ").lower()
try:
number=int(number)
if number <= 0 or number > len(people):
print("Number is out of range ")
elif number > 0 or number <= len(people):
break
except:
print("invalid number")
people.pop(number-1)
print("person deleted")
return people
def Search_person(people):
search_name=input(" enter the search name: ").lower()
result=[]
for person in people:
name=person["name"]
if search_name in name.lower():
result.append(person)
get_people(people)
def list(people):
for i,person in enumerate(people):
print(i+1,person["name"],person["mobilenumber"])
print("Hi Welcome to Contact Management System.")
with open("contacts.json","r") as f:
people=json.load(f)["contacts"]
while True:
print("contact size: ",len(people))
operation=input(" you can add/delete/search/show and 'Q' for quit :").lower()
if operation=="add":
person=add_person()
people.append(person)
print("person added ! ")
elif operation=="delete":
delete(people)
elif operation == "search":
Search_person(people)
elif operation=="show":
list(people)
elif operation=="q":
break
else:
print("invalid command")
with open("contacts.json","w") as f:
people=json.dump({"contacts": people},f)