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>First thing, you have to declare phone_contacts and the functions outside of the loop.</p>

<p>Second there are redundancies in the conditions.</p>

<p>The idea of creating a file to store contacts is great. I would save it as a <code>.json</code> file as it is very simple to handle.</p>

<p>Here is the code, which I have refactored as well as I could.</p>

<pre><code>import json


phone_contacts = {}


def print_contacts():
    if not phone_contacts:
        print("Empty list.")
        return

    for name, number in sorted(phone_contacts.items()):
        print(name, number)  # explicit is better than implicit


def add_contact():
    name = input("Enter a name\n")
    number = input("Enter a number\n")
    if name and number:
        phone_contacts[name] = number
        print("Contact added: {0}, {1}".format(name, number))


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


def load_contacts():
    global phone_contacts
    try:
        with open('contacts.json', 'r') as file:
            phone_contacts = json.load(file)
    except (ValueError, OSError):  # OSError catches FileNotFoundError
        phone_contacts = {}


def save_contacts():
    with open('contacts.json', 'w+') as file:
        json.dump(phone_contacts, file)


load_contacts()
while True:
    print("0. Exit")
    print("1. Menu")
    print("2. Add contact")
    print("3. Remove contacts")

    choose = input("Select option: ")

    if choose == "0":
        print("Exiting program...")
        break
    elif choose == "1":
        print_contacts()
    elif choose == "2":
        add_contact()
    elif choose == "3":
        remove_contacts()
    else:
        print("You didn't type a valid option!")

    # moved this block out as it's common to all options
    choose_2 = input("End/Back to MENU\n").lower()
    if choose_2 == "end":
        break
    # even if user typed anything different of menu
    # he/she would continue in the loop so that else was needless
save_contacts()
</code></pre>

<p>Also note that you don't need to pass phone_contacts as an argument since it's global.</p>

<p>I've added load and save contacts functions that is pretty understandable, even if you have no experience with JSON.</p>

<p>There's a lot of things to ask there so if you have some doubt and want to ask me, feel comfortable! ;)</p>

python python-3.x dictionary