Question: When adding contacts, an appropriate error message as shown below should be displayed if the user enters an invalid email address ( i . e
When adding contacts, an appropriate error message as shown below should be displayed if the user enters an invalid email address ie does not follow format, x@xx
When adding contacts, an appropriate error message as shown below should be displayed if the user enters an invalid phone number ie includes letters, or special characters other than a dash
import csv
# Function to load contacts from the CSV file
def loadcontacts:
contacts
try:
with opencontactscsv moder as file:
reader csvDictReaderfile
for row in reader:
contacts.appendrow
printContacts file loaded."
except FileNotFoundError:
printNo contacts found. Starting fresh."
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."
def addemailcontacts:
email inputEmail:
try:
if not in email:
printInvalid email address"
elif @ not in email:
printInvalid email address"
class NonalphaErrorException:
pass
class NondigitErrorException:
pass
def validatenamephoneno:
try:
ifnameisalphaTrue:
raise NonalphaError
ifphonenoisdigitTrue:
raise NondigitError
except NonalphaError:
printPlease Enter Alphabets only"
except NondigitError:
printPlease Enter Digits only"
printThe contact ofname,"isphoneno
finally
main
# 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
