0

I don't have a lot of experience with python but I think this can be much simpler. If anything available for the same result. Using a dictionary mapping to functions instead of all those elif maybe?

    choice = input("Select an option: ")

    if choice == "1":
        try:
            new_contact = create_contact()
        except NotAPhoneNumberException:
            print("The phone number entered is invalid, creation aborted!")
        else:
            contacts[new_contact['name']] = new_contact
            save_contacts(contacts, filename)

    elif choice == "2":
        print_contact()

    elif choice == "3":
        search = input("Please enter name (case sensitive): ")
        try:
            print_contact(contacts[search])
        except KeyError:
            print("Contact not found")


    elif choice == "0":
        print("Ending Phone Book.\nHave a nice day!")
        break

    else:
        print("Invalid Input! Try again.")
  • Possible duplicate of [Replacements for switch statement in Python?](https://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python) – L3viathan Jan 11 '18 at 10:41
  • The possible duplicate question isn't exactly the same, but it might help you nevertheless. TL;DR: Python doesn't have a switch statement, you may use a dictionary mapping to functions, but most of the time, if-elif-elif-elif... probably is the most readable. – L3viathan Jan 11 '18 at 10:42

0 Answers0