Text-based contact management
Project Description
Create a program that a user can use to manage the primary contacts.
Sample Run
Contact Management System
COMMAND MENU
list - Display all contacts
view - View a contact
add - Add a contact
del - Delete a contact
exit - Exit program
Command: list
1. Betty Johnson, 3333
2. Mark Arlington, 4444
Command: view
Number (1 - 2): 3
Invalid number. Try again!
Command: view
Number (1 - 2): 2
Mark Arlington, 4444
Command: add
Name: Andy Patel
Email: andy.patel@hotmail.com
Phone: (415) 222-3333
ID: 5555
Andy Patel, 5555 was added.
Command: del
Number (1 - 3): 0
Invalid number. Try again!
Command: del
Number (1 - 3): 1
Betty Johnson, 3333 was deleted.
Command: list
1. Mark Arlington, 4444
2. Andy Patel, 5555
Command: exit
Thank you for using my app
Specifications
- Your program should have the following functions:
- def display(contacts)
- def add(contacts)
- def view(contacts)
- def delete(contacts)
- def display_title()
- def display_menu()
- def main()
- Use a list of lists (2-dimensional list) to store the data for the contacts. Provide starting data for the following contacts:
- "Betty Johnson", "betty.johnson@gmail.com", "(408) 111-2222", "3333"
- "Mark Arlington", "mark.arlington@yahoo.com", "(669) 123-4567", "4444"
- Your program should display the current number of contacts on the list so that the user knows how to enter valid data. For example, if the list has 3 contacts, your program should display "Number (1 - 3): ".
- Note that the first index of the 2-dimensional list is 0, but the first contact starts with 1
Solution:
'''
def display(contacts):
# Checking the list contacts to see if there are any contacts added
# If there are no contacts, length of list will be 1
if len(contacts) == 1:
print('No contacts added. Please add a contact first')
return None
# Printing the list with its order number
# enumerate() is used to provide serial numbers to container elements
for num, contact in enumerate(contacts[1:], 1):
print('{}. {}, {}'.format(num, contact[0], contact[-1]))
def add(contacts):
name = input('Name: ')
email = input('Email: ')
phone = input('Phone: ')
contact_id = input('ID: ')
# Adding the user details taken via input to contacts list
contacts.append([name, email, phone, contact_id])
print('{}, {} was added.'.format(name, contact_id))
def view(contacts):
# Checking the list contacts to see if there are any contacts added
# If there are no contacts, length of list will be 1
if len(contacts) == 1:
print('No contacts added. Please add a contact first')
return None
choice = int(input('Number ({} - {}): '.format(1, len(contacts)-1)))
# Checking if serial number entered is valid or not
if choice < 1 or choice >= len(contacts):
print('Invalid Number. Try again!')
return None
print('{}, {}'.format(contacts[choice][0], contacts[choice][-1]))
def delete(contacts):
# Checking the list contacts to see if there are any contacts added
# If there are no contacts, length of list will be 1
if len(contacts) == 1:
print('No contacts added. Please add a contact first')
return None
# Checking if serial number entered is valid or not
choice = int(input('Number ({} - {}): '.format(1, len(contacts)-1)))
if choice < 1 or choice >= len(contacts):
print('Invalid Number. Try again!')
return None
# Deleting the specified contact
del contacts[choice]
def display_title():
print('Contact Management System')
def display_menu():
# Printing the Display Menu
print()
print()
print('COMMAND MENU')
print('list - Display all contacts')
print('view - View a contact')
print('add - Add a contact')
print('del - Delete a contact')
print('exit - Exit program')
print()
def main():
display_title()
# Initializing contacts(list of list) with an empty list
# So that contacts added will start with index 1
# If length of contacts list is 1, it is essentially empty
contacts = [[]]
command = 'None'
while command != 'exit':
display_menu()
command = input('Command: ')
valid_commands = ['list', 'view', 'add', 'del', 'exit']
# Checking if the input command is valid or not
if command not in valid_commands:
print('Invalid Command. Try again!')
continue
if command == 'list':
# Calling the display function
display(contacts)
elif command == 'view':
# Calling the view function
view(contacts)
elif command == 'add':
# Calling the add function
add(contacts)
elif command == 'del':
# Calling the delete function
delete(contacts)
elif command == 'exit':
print()
print('Thank you for using my app')
break
if __name__ == '__main__':
main()