I'm a python beginner tried making a contact book/address book program. I take a user input/contact info(value) and store it as a value to a dictionary, however while the program still running if I try to enter a new contact info(new value) it will overwrite the existing one, how do I solve this? so I can add as many contact info(value) as I want while the program still running.
Thanks in advance.
this is my code:
def head():
print("")
print("========================")
print(" Contact Book ")
print("========================")
def restart():
response = input("\nOpen menu again? (yes/no): ").lower()
if response == "yes":
task()
else:
print("\nSee You next time!")
def task():
head()
done = False
print('''1. Add Contact
2. Search
3. View Contact List
4. Delete All Contact
5. Exit''')
while not done:
task = input("\nWhat do You want to do? (1-5):")
if task == "1":
print("\nAdding a new contact!")
global cnt_lst
global new
cnt_lst = {}
new = {}
new['new_key'] = {}
new['new_key']['Name '] = input("Name: ")
new['new_key']['Phone'] = input("Phone: ")
if not new['new_key']['Phone'].isnumeric():
while not new['new_key']['Phone'].isnumeric():
print("Invalid input, please enter only a number!")
new['new_key']['Phone'] = input("Phone: ")
new['new_key']['Email'] = input("Email: ")
cnt_lst.update(new)
print("\nContact is saved!")
done = True
restart()
elif task == "2":
search = input("\nSearch: ")
info = False
for key, value in cnt_lst.items():
for npe, val in value.items():
if search in val:
info = True
print("=" * 20)
for npe, val in value.items():
print(npe,' : ',val)
break
if not info:
print("\nNo info was found!")
done = True
restart()
elif task == "3":
if 'cnt_lst' not in globals():
print("\nNo contact info available!")
elif 'cnt_lst' in globals() and len(cnt_lst) == 0:
print("\nNo contact info available!")
else:
print("\nAll Contact Info")
for key, value in cnt_lst.items():
for npe, val in value.items():
print("=" * 20)
for npe, val in value.items():
print(npe,' : ',val)
break
done = True
restart()
elif task == "4":
cnt_lst.clear()
print("\nSuccesfully deleted all contact info!")
done = True
restart()
elif task == "5":
print("See You next time!")
break
else:
print("Invalid input please enter a single number from 1 to 5")
restart()
task()