Question: How do I add exception handling if these are my inputs? command: view number: 2 ( invalid contact number ) Command: view Number x: (
How do I add exception handling if these are my inputs?
command: view
number: invalid contact number
Command: view
Number x: Invalid integer
Command: del
Number: invalid contact numbers
Command: del
Number: invalid integer
Command: list There are no contacts in the list
Command: view There are no contacts in the list
Command: del There are no contacts in the list
Command: exit Bye
Here's what I have so far:
import csv
import os
contacts
import csv
import os
if not ospath.isfilecontactscsv:
with opencontactscsvw newline as file:
writer csvwriterfile
printcontactscsv file not found. A new file has been created.
else:
printcontactscsv file found.
def loadcontacts:
contacts
try:
with opencontactscsv moder as file:
reader csvDictReaderfile
for row in reader:
contacts.appendrow
printContacts file loaded."
except FileNotFoundError:
printCould not find contacts file!"
printCreating new contacts file..."
return contacts
# Function to save contacts to the CSV file
def savecontactscontacts:
with opencontactscsv modew newline as file:
fieldnames Name 'Email', 'Phone'
writer csvDictWriterfile fieldnamesfieldnames
writer.writeheader
for contact in contacts:
writer.writerowcontact
# Function to display the command menu
def displaymenu:
print
COMMAND MENU"
printlist Display all contacts"
printview View a contact"
printadd Add a contact"
printdel Delete a contact"
printexit Exit program"
# Function to display all contacts
def listcontactscontacts:
if not contacts:
printNo contacts to display."
else:
for i contact in enumeratecontacts start:
printficontactName
# Function to view a specific contact
def viewcontactcontacts:
try:
number intinputNumber:
contact contactsnumber
printfName: contactName
printfEmail: contactEmail
printfPhone: contactPhone
except ValueError IndexError:
printInvalid contact number."
# Function to add a contact
def addcontactcontacts:
name inputName:
email inputEmail:
phone inputPhone:
contacts.appendName: name, 'Email': email, 'Phone': phone
savecontactscontacts
printfname was added."
# Function to delete a contact
def deletecontactcontacts:
try:
number intinputNumber:
contact contacts.popnumber
savecontactscontacts
printfcontactName was deleted."
except ValueError IndexError:
printInvalid contact number."
# Main function to run the program
def main:
contacts loadcontacts
displaymenu
while True:
command input
Command: lower
if command "list":
listcontactscontacts
elif command "view":
viewcontactcontacts
elif command "add":
addcontactcontacts
elif command "del":
deletecontactcontacts
elif command "exit":
break
else:
printInvalid command. Try again."
displaymenu
# Start the program
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
