Question: This question is for a program in python Below is my program code for my contact manager. What I need help with is the following
This question is for a program in python
Below is my program code for my contact manager. What I need help with is the following (to add to my code):
- If the program cant find the CSV file, it should display an appropriate message and create a new CSV file that doesnt contain any contact data.
- For the view and del commands, display an appropriate error message if the user enters an invalid integer or an invalid contact number.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is my code for my contact manager:
##my program code
import csv,os;
##definition name for the CSV file
FILENAME = "contacts.csv"
##definiton write function and write items to CSV file
def write_contacts(contacts): with open (FILENAME, "w", newline="") as file: writer = csv.writer(file) writer.writerows(contacts)
##definition read items from CSV file into a list
def read_contacts(): contacts = [] with open(FILENAME, newline="") as file: reader = csv.reader(file) for row in reader: contacts.append(row) return contacts
##definition list command
def list_contacts(contacts): for i in range(len(contacts)): contact = contacts[i] print(str(i+1) + "." + contact[0]) print()
##definition view command
def view_contacts(contacts): number = int(input("Number: ")) if number len (contacts): print("invalid number") else: contact = contacts[number-1] print("Name: " + contact[0]) print("Email: " + contact[1]) print("Phone: " + contact[2]) print() ##definition add command
def add_contacts(contacts): name = input("Name: ") email = input("Email: ") phone = input("Phone: ") contact = [] contact.append(name) contact.append(email) contact.append(phone) contacts.append(contact) write_contacts(contacts) print(name + " was added") print()
##definition delete command
def del_contacts(contacts): number = int(input("Number: ")) if number len (contacts): print("invalid number") else: contact = contacts.pop(number-1) write_contacts(contacts) print(contact[0] + " was deleted") return contacts
def display_menu(): print("Contact Manager") print() print("COMMAND MENU") print("list - Display all contacts", " view - View a contact", " add - Add a contact", " del - Delete a contact", " exit - Exit program") print()
##definition main menu
def main(): display_menu() contacts = read_contacts() while True: command = input("Command: ") if command.lower() == "list": list_contacts(contacts) elif command.lower() == "view": view_contacts(contacts) elif command.lower() == "add": add_contacts(contacts) elif command.lower() == "del": del_contacts(contacts) elif command.lower() == "exit": break else: print("Invalid command. Please try again. ") print("Good bye!") if __name__ == "__main__": main()
-------------------------------------------------------------------------------------------------------------------------------------------
Here is an example of how the outcome should look:

Could not find contacts file! Starting new contacts file. . . Contact Manager COMMAND MENU list - Display all contacts view - View a contact Add a contact - Delete a contact del exit - Exit program Command: list There are no contacts in the list. Command: add Name: Bugsy Lapin Email: blapin@bunnies.org Phone: 352-555-2468 Bugsy Lapin was added Command: view Number: 8 Invalid contact number Command: view Number: B Invalid integer. Command: view Number: 1 Name: Bugsy Lapin Email: blapin@bunnies.org Phone: 352-555-2468 Could not find contacts file! Starting new contacts file. . . Contact Manager COMMAND MENU list - Display all contacts view - View a contact Add a contact - Delete a contact del exit - Exit program Command: list There are no contacts in the list. Command: add Name: Bugsy Lapin Email: blapin@bunnies.org Phone: 352-555-2468 Bugsy Lapin was added Command: view Number: 8 Invalid contact number Command: view Number: B Invalid integer. Command: view Number: 1 Name: Bugsy Lapin Email: blapin@bunnies.org Phone: 352-555-2468
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
