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: