Write a program that keeps a person’s name and his/her contact number in a dictionary as key- value pairs. The program should display a menu that lets the user:
a. see a list of all contacts and their numbers,
b. add a new contact and number,
c. change the number of an existing contact, and
d. delete an existing contact and number.
The program should save the dictionary when the user exits the program. Each time the program starts, it should retrieve the dictionary from the file.
For more information on file reading & writing in Python, please have a look at these websites:
https://www.geeksforgeeks.org/how-to-read-from-a-file-in-python/?ref=lbp https://www.geeksforgeeks.org/writing-to-file-in-python/?ref=lbp
print("""
a. see a list of all contacts and their numbers,
b. add a new contact and number,
c. change the number of an existing contact, and
d. delete an existing contact and number.
""")
choices = input('Enter your choices here: ')
def see_list(n):
dic = {}
for i in range(n):
dic[input('Enter friend''s name' )] = input('Enter phone number ')
return dic
def add_name(n):
dic = see_list(n)
dic[input('Enter friend''s name ' )] = input('Enter phone number ')
def mod_name(str1, dic):
dic[str1] = input('Enter new number here ')
def del_num(str2, dic):
dic.pop(str2, None)
if choices == 'a':
i = int(input('Enter number of friends here: '))
print(see_list(i))
elif choices == 'b':
i = int(input('Enter number of friends here: '))
add_name(i)
elif choices == 'c':
i = int(input('Enter number of friends here: '))
str1 = input('Enter name to be modified here: ')
dic = see_list(i)
mod_name(str1, dic)
elif choices == 'd':
i = int(input('Enter number of friends here: '))
str2 = input('Enter name to be modified here: ')
dic = see_list(i)
del_num(str2, dic)
Comments
Leave a comment