Paweł.K Paweł.K - 2 months ago 19
Python Question

Phone menu - Python 3

I'm trying to make some project of phone Menu, with viewing, adding and removing contacts. Which way would be better, to add contact to dictionary or by making new file and write/read from it?

I tried first method - I had added new contact to empty dictionary and then I tried to view it by using viewing option but still dictionary is empty.

That's a first sample of my "project":

condition = True


while condition == True:
print("Menu")
print("contacts")
print("Add contacts")
print("Remove contacts")

phone_contacts = {}

def contacts(x):
for item in dict(x):
print(item, x[item])


def add_contacts(x):
new_key = input("Enter a name\n")
new_value = input("Enter a number\n")
x[new_key] = new_value
print(phone_contacts)


def remove_contacts(x):
for item in dict(x):
print(item)
removing_contact = input("Enter a contact to remove\n")
if removing_contact in x:
del x[removing_contact]
print("Contact has been deleted!")
else:
print("There is no '%s\' contact!") % (removing_contact)


choose = input("Select option")

if choose == "1":
print(contacts(phone_contacts))
choose_2 = input("End/Back to MENU").lower()
if choose_2 == "end":
break
elif choose_2 == "menu":
pass

elif choose == "2":
print(add_contacts(phone_contacts))
choose_2 = input("End/Back to MENU").lower()
if choose_2 == "end":
break
elif choose_2 == "menu":
pass

elif choose == "3":
print(remove_contacts(phone_contacts))
choose_2 = input("End/Back to MENU").lower()
if choose_2 == "end":
break
elif choose_2 == "menu":
pass
else:
print("You didn't type anything!")
choose_2 = input("End/Back to MENU").lower()
if choose_2 == "end":
break
elif choose_2 == "menu":
pass


So this way doesn't work, I tried also write into a text.txt file

condition = True


while condition == True:
print("Menu")
print("contacts")
print("Add contacts")
print("Remove contacts")

phone_contacts = {}

def contacts(x):
for item in dict(x):
print(item, x[item])


def add_contacts(x):
new_key = input("Enter a name\n")
new_value = input("Enter a number\n")
text = "%s - %d" % (new_key, int(new_value))
savefile = open("text.txt", "w")
savefile.write(text)
savefile.read(text)
savefile.close()

def remove_contacts(x):
for item in dict(x):
print(item)
removing_contact = input("Enter a contact to remove\n")
if removing_contact in x:
del x[removing_contact]
print("Contact has been deleted!")
else:
print("There is no '%s\' contact!") % (removing_contact)


choose = input("Select option")

if choose == "1":
print(contacts(phone_contacts))
choose_2 = input("End/Back to MENU").lower()
if choose_2 == "end":
break
elif choose_2 == "menu":
pass

elif choose == "2":
print(add_contacts(phone_contacts))
choose_2 = input("End/Back to MENU").lower()
if choose_2 == "end":
break
elif choose_2 == "menu":
pass

elif choose == "3":
print(remove_contacts(phone_contacts))
choose_2 = input("End/Back to MENU").lower()
if choose_2 == "end":
break
elif choose_2 == "menu":
pass
else:
print("You didn't type anything!")
choose_2 = input("End/Back to MENU").lower()
if choose_2 == "end":
break
elif choose_2 == "menu":
pass


It doesn't work neither.

What am I doing wrong in both cases? Which way should I choose, first or the second one?

BTW I would be grateful for any tips how I could correct my code, even if those tips doesn't pertains to the problem.

Answer

In both options you are overwriting the dictionary on every iteration. You should initilize it only once outside the loop. For the first option it will look like:

phone_contacts = {}             # this line moved from inside the loop

while condition == True:
    print("Menu")
    print("contacts")
    print("Add contacts")
    print("Remove contacts")

                                # the line deleted from here

    # ...