-1

The assignment is to create a program that allows user to enter friend's name and phone number then print out contact list sorted by last name. Also to use function.

My problem is it only asks the user to make one action and then it just ask for details right away. It should ask the user to choose another action. Either to exit, add contact, show contacts or sort contacts.

def menu():
  '''Display Menu'''
  print(
    """

    Contact Lists

    0 - Exit
    1 - Show Contacts
    2 - Add Contacts
    3 - Sort Contacts
    """
    )

def ask():
  user = None
  user = input("Action: ")
  print()
  return user

def main():
  menu()
  action = ask()
  names = []

  while action != 0:
    if action == "0":
      print("Closing Contact Lists.")

    elif action == "1":
      print("Contact Lists: ")
      for name in names:
        print(name)
#setting a condition if user enter "2" it will let user add name, last name and phone number
    elif action == "2":
        name = input("Add contact's first name: ") #input 1
        last_name = input("Add contact's last name: ") #input 2
        contact_number = input("Add phone number for the contact name: ") #input 3
        entry = (last_name, name, contact_number)
        names.append(entry)

#setting a condition if user enter "3" it will sort contact list according to last names
    elif action == "3":
        entry.sort(reverse=True) #use of sort() to sort lists of by last names
        print(names)
    else:
        print("Invalid Action!") 

main()
abhilb
  • 5,639
  • 2
  • 20
  • 26
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – DeepSpace Dec 11 '19 at 06:42

1 Answers1

0

Two errors in your code:

  1. You should read user's input at the end of every loop.

Try to add action = ask() below your last else condition.

edit

code pieces:


#setting a condition if user enter "3" it will sort contact list according to last names
    elif action == "3":
        entry.sort(reverse=True) #use of sort() to sort lists of by last names
        print(names)
    else:
        print("Invalid Action!") 
    action = ask()
  1. sort() should be performed on names(a list), not entry(a tuple)
lincr
  • 1,633
  • 1
  • 15
  • 36