Skip to content

Commit 2413026

Browse files
Create Dictionaries.py
1 parent b6de8e6 commit 2413026

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
person = {'first_name': 'Bruce', 'last_name': 'Wayne', 'age': 25
2+
}
3+
# print(person['first_name'])
4+
# print(person['fav_color'])
5+
person['fav_color'] = 'blue'
6+
person['fav_color'] = 'red'
7+
person.update({'first_name': 'Robin', 'age': 33, 'weight': 200})
8+
#
9+
# print(person['DOB'])
10+
# print(person.get('DOB', 'Not found'))
11+
12+
#keys(), values(), items()
13+
#finding all keys from dict
14+
attributes = person.keys()
15+
# print(attributes, 'print key attr')
16+
17+
#find all values
18+
values = person.values()
19+
# print(values, 'printing values')
20+
21+
#key/value pairs
22+
pairs = person.items()
23+
# print(pairs, 'key/value')
24+
25+
#print the keys
26+
# for x in person:
27+
# print(x)
28+
29+
#print keys and values
30+
# for key, value in person.items():
31+
# print(key, value)
32+
33+
34+
#append data to contacts list
35+
# contacts.append(name)
36+
# contacts.append(phone_number)
37+
# contacts.append(fav_color)
38+
39+
40+
# contacts = []
41+
# while True:
42+
# name = input('Enter a name of contact: ')
43+
# phone_number =input(f"Enter {name}'s phone number: ")
44+
# fav_color = input(f"Enter {name}'s favorite color: ")
45+
46+
# contacts.append({
47+
# 'name': name, 'phone_number': phone_number, 'fav_color': fav_color
48+
# })
49+
50+
# additional_data = input('Enter another contact? y/n')
51+
# if additional_data =='n':
52+
# break
53+
# print(contacts)
54+
55+
56+
contacts = [
57+
{'name': 'Susan', 'phone_number': 5555555555, 'fav_color': 'green'},
58+
{'name': 'Bob', 'phone_number': 555555885, 'fav_color': 'blue'},
59+
{'name': 'Tom', 'phone_number': 4555555555, 'fav_color': 'yellow'}
60+
61+
]
62+
63+
64+
# print(contacts, 'entire list of dicts')
65+
# for contact in contacts:
66+
# print(contact['name'], 'values from key')
67+
68+
# for contact in contacts:
69+
# if contact['name'] == 'Susan':
70+
# print(contact['name'], 'conditional value')
71+
72+
while True:
73+
#show menu to user
74+
choice = input('Choose: add, search, update, delete ')
75+
#if user keys 'add', prompt for new contact info
76+
if choice == 'add':
77+
name = input('Enter a name of contact: ')
78+
phone_number =input(f"Enter {name}'s phone number: ")
79+
fav_color = input(f"Enter {name}'s favorite color: ")
80+
81+
#append contact info to the contacts list
82+
contacts.append({
83+
'name': name, 'phone_number': phone_number, 'fav_color': fav_color
84+
})
85+
86+
elif choice == 'search':
87+
search_name = input('Enter contact name ').lower()
88+
#loop over all contacts
89+
for contact in contacts:
90+
#if search name matches name in contact, print info
91+
if contact['name'].lower() == search_name:
92+
print(contact['name'])
93+
print(contact['phone_number'])
94+
print(contact['fav_color'])
95+
96+
elif choice == 'update':
97+
search_name = input('Enter contact name: ').lower()
98+
#loop over contacts dictionary
99+
for contact in contacts:
100+
if contact['name'].lower() == search_name:
101+
#check if name is correct
102+
correct = input(f"Is {contact['name']} correct? y/n")
103+
# if not correct, ask for data
104+
if correct == 'n':
105+
name = input(f"Enter new/revised name for contact")
106+
contact['name'] = name
107+
#check if phone number is correct
108+
correct = input(f"Is {contact['phone_number']} correct? y/n")
109+
# if not correct, ask for data
110+
if correct == 'n':
111+
phone_number = input(f"Enter new/revised phone_number for contact")
112+
contact['phone_number'] = phone_number
113+
#check if fav_color is correct
114+
correct = input(f"Is {contact['fav_color']} correct? y/n")
115+
# if not correct, ask for data
116+
if correct == 'n':
117+
color = input(f"Enter new/revised color for contact")
118+
contact['fav_color'] = color
119+
120+
#show updated contact info
121+
print(contact['name'])
122+
print(contact['phone_number'])
123+
print(contact['fav_color'])
124+
125+
elif choice == 'delete':
126+
for i, contact in enumerate(contacts): #[i = 0{susan}, i = 1 {bob}]
127+
print(f" {i} - {contact['name']}")
128+
index = input('Enter the number of the contact u want 2 delete: ')
129+
remove_contact = contacts.pop(int(index))
130+
131+
132+
additional_data = input('Enter another contact? y/n ')
133+
if additional_data =='n':
134+
break
135+
136+
print(contacts)

0 commit comments

Comments
 (0)