Phone menu - Python 3
<p>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?</p>

<p>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.</p>

<p>That's a first sample of my "project":</p>

<pre><code>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
</code></pre>

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

<pre><code>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
</code></pre>

<p>It doesn't work neither. </p>

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

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

<p>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:</p>

<pre><code>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

    # ...
</code></pre>

python python-3.x dictionary