Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Omar Abu Hamdan
794 Pointsdictionaries
def menu(): print("""1-Add a new name and email address 2-Change an existing email address 3-Delete an existing name and email address 4-Look up a person's email address 5-Exit""")
def main():
menu()
choice=int(input("Please enter your choice from the menu: "))
name_dict={}
while choice!=5:
name_dict={'omar':['omarabuhamdan@yahoo.com'],'fadel':['fahtrading@yahoo.com']}
if choice==1:
name=input("Please input name: ")
email=input("Please input email address: ")
if name in name_dict:
print("This name already exists!")
else:
name_dict[name]=[email]
print(name_dict)
main()
elif choice==2:
name=input("please input the name you want to edit: ")
if name in name_dict:
email=input("please enter the new email: ")
name_dict[name]=[email]
print(name_dict)
else:
print("This name doesn't exist")
main()
elif choice==3:
name=input("Please enter the name you would like to delete: ")
if name in name_dict:
del name_dict[name]
print(name_dict)
else:
print("This name doesn't exist")
main()
elif choice==4:
name=input("Please enter the name you would like to search for: ")
if name in name_dict:
name_dict.get(name)
else:
print("This name doesn't exist")
main()
else:
exit()
main() i am having a problem with my code and I'm not being able to figure it out. my dictionary isn't saving what I'm writing.
1 Answer
Abhishek Tayal
21,525 PointsYou are calling the main() function every time when you are adding/modifying/deleting the entry in the dictionary. And that's why, every time you are starting with fresh name_dict. Please delete the main() function call from each case in while loop. def main():
choice=int(input("Please enter your choice from the menu: "))
name_dict={}
while choice!=5:
if choice==1:
name=input("Please input name: ")
email=input("Please input email address: ")
if name in name_dict:
print("This name already exists!")
else:
name_dict.update({name: [email]})
print(name_dict)
elif choice==2:
name=input("please input the name you want to edit: ")
if name in name_dict:
email=input("please enter the new email: ")
name_dict.update({name: [email]})
print(name_dict)
else:
print("This name doesn't exist")
elif choice==3:
name=input("Please enter the name you would like to delete: ")
if name in name_dict:
del name_dict[name]
print(name_dict)
else:
print("This name doesn't exist")
elif choice==4:
name=input("Please enter the name you would like to search for: ")
if name in name_dict:
name_dict.get(name)
else:
print("This name doesn't exist")
else:
exit()
main()
Omar Abu Hamdan
794 Pointsit still doesn't save the library though, and other than that it keeps on asking me to input the name rather than moving on and giving me the menu again.
Omar Abu Hamdan
794 PointsOmar Abu Hamdan
794 Pointsi was testing it that's why i got some names and emails already in there, but it should be empty and i could fill it with names and emails as key-value pairs