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 (i.e., does not follow format, x@x.x.
When adding contacts, an appropriate error message as shown below should be displayed if the user enters an invalid phone number (i.e., includes letters, or special characters other than a dash).
import csv
# Function to load contacts from the CSV file
def load_contacts():
contacts =[]
try:
with open('contacts.csv', mode='r') as file:
reader = csv.DictReader(file)
for row in reader:
contacts.append(row)
print("Contacts file loaded.")
except FileNotFoundError:
print("No contacts found. Starting fresh.")
return contacts
# Function to save contacts to the CSV file
def save_contacts(contacts):
with open('contacts.csv', mode='w', newline='') as file:
fieldnames =['Name', 'Email', 'Phone']
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for contact in contacts:
writer.writerow(contact)
# Function to display the command menu
def display_menu():
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")
# Function to display all contacts
def list_contacts(contacts):
if not contacts:
print("No contacts to display.")
else:
for i, contact in enumerate(contacts, start=1):
print(f"{i}.{contact['Name']}")
# Function to view a specific contact
def view_contact(contacts):
try:
number = int(input("Number: "))
contact = contacts[number -1]
print(f"Name: {contact['Name']}")
print(f"Email: {contact['Email']}")
print(f"Phone: {contact['Phone']}")
except (ValueError, IndexError):
print("Invalid contact number.")
# Function to add a contact
def add_contact(contacts):
name = input("Name: ")
email = input("Email: ")
phone = input("Phone: ")
contacts.append({'Name': name, 'Email': email, 'Phone': phone})
save_contacts(contacts)
print(f"{name} was added.")
def add_email(contacts):
email = input("Email: ")
try:
if '.' not in email:
print("Invalid email address")
elif '@' not in email:
print("Invalid email address")
class NonalphaError(Exception):
pass
class NondigitError(Exception):
pass
def validate(name,phone_no):
try:
if(name.isalpha()!=True):
raise NonalphaError
if(phone_no.isdigit()!=True):
raise NondigitError
except NonalphaError:
print("Please Enter Alphabets only")
except NondigitError:
print("Please Enter Digits only")
print("The contact of",name,"is",phone_no)
finally
main()
# Function to delete a contact
def delete_contact(contacts):
try:
number = int(input("Number: "))
contact = contacts.pop(number -1)
save_contacts(contacts)
print(f"{contact['Name']} was deleted.")
except (ValueError, IndexError):
print("Invalid contact number.")
# Main function to run the program
def main():
contacts = load_contacts()
display_menu()
while True:
command = input("
Command: ").lower()
if command == "list":
list_contacts(contacts)
elif command == "view":
view_contact(contacts)
elif command == "add":
add_contact(contacts)
elif command == "del":
delete_contact(contacts)
elif command == "exit":
break
else:
print("Invalid command. Try again.")
display_menu()
# Start the program
if __name__=="__main__":
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!