Home > Computer Programming > Command line contact management application in Python

Command line contact management application in Python

October 14, 2014 Leave a comment Go to comments

Some of you were looking slightly fazed by my explanations in class today, so here is the code for the simplest contact manager application you could imagine. This one has no disk storage, but it should give you a reasonable idea of how to:

  • use a simple class
  • create and call basic functions
  • iterate over a list
class Contact:
    name = ""
    number = ""

def addContact():
    print()
    contact = Contact()
    contact.name = input("Enter contact name: ")
    contact.number = input("Enter contact number: ")
    contactList.append(contact)
    print("Contact added.")
    print()
    
def getMenuChoice():
    print("1. Add new contact")
    print("2. Print all contacts")
    print("3. Quit")
    return input("Please enter your choice (1-3): ")

def printContacts():
    print()
    print("Printing contacts...")
    for itm in contactList:
        print (itm.name + ", " + itm.number),
    print()

#MAIN ROUTINE
contactList = []
choice = getMenuChoice()
while choice != "3":
    if choice == "1":
        addContact()
    elif choice == "2":
        printContacts()
    choice = getMenuChoice()
print()
print("Goodbye.")

Here is what the program looks like when it runs:

python contact output

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s