Question: Exercise In this exercise, you will create a menu-driven application to manage contacts that are stored in a text file. The file named contacts.txt is

Exercise

In this exercise, you will create a menu-driven application to manage contacts that are stored in a text file. The file named "contacts.txt" is provided to download.

Specifications:

You will need to create separate functions to:

House the main() code

Display an option menu as shown in the example output below

Read and print the contacts stored in the text file

Add a contact to the file

Delete a contact from the file

The display_menu() function should

Print a list of choices for the program

The main() function should:

Prompt the user to input the name of the contact text file and store it in a variable. You need to use a "try/except" statement when you attempt to open the file. Use the "FileNotFoundError" type in your exception and print a message if this occurs. Place this entire section of the code in a "While" loop so that if an error occurs it repeatedly ask for a file input. When the file opens successfully you can first close the file and then exit the loop. This part is just a test to make sure the file exists and is readable.

Call the function to display the menu

Inside a second While loop you will use an input for the user to input a menu choice. Use an "If/elif/elif/elif/else" code block to determine which function to call based on the user's input. If they enter 'exit' then break the while loop and say "Good-Bye". The "else" should contain a warning that they didn't enter one of the choices correctly. NOTE: you'll need to pass the file name variable as parameter to the view, add, and delete functions when you call them

NOTE: In the functions below you must have a parameter to receive the file name value. When you call these functions from the main() you'll need to pass in that file name variable from step #1 above as an argument.

The view_contacts(contact_file) function should receive the file name as a parameter.

Open the file for reading

Loop through the contents of the file printing each line to the console.

Initialize a counter variable and increment it inside the loop. Concatenate the number and a period to the beginning of each line

Close the file, and call the function to display the menu again. NOTE: use a blank print() to create spacing as needed

The add_contacts(contact_file) function should receive the file name as a parameter.

Prompt the user for name, email, and phone number and store the inputs into variables

Concatenate the three items separated by commas with a " " newline character at the end

Open the file for appending and write the new line to the file

Close the file, and call the function to display the menu again. NOTE: use a blank print() to create spacing as needed

Print a success message, a space and the menu again.

The delete_contacts(contact_file) function should receive the file name as a parameter.

Call the view() function to display the lines again so the user can be reminded of the numbers associated with each line

Prompt the user to enter the number of the item they want to delete - store that number in a variable.

Open the file for reading and Loop through the contents of the file. Make sure to initialize a counter variable , just like in the view() function, and increment it inside the loop so that it corresponds to the line items as viewed previously.

Inside the loop, use an "if" statement to compare the counter with the user's choice for deletion. Build a new string variable from all of the line items that DON'T match the user's input (e.g. x = x + 'new item').

After the loop, close the file

Open the file again, but this time for writing and write the newly created string variable to the file.

Print a success message, a space and the menu again.

Sample input/output: (all user inputs are in bold)

Contact Manager

Enter Contacts File Name: contacts.txt COMMAND MENU view - View a contact add - Add a contact del - Delete a contact exit - Exit program

Command: view 1. Guido van Rossum, guido@python.org, 123-456-7890 2. Eric Idle, eric@ericidle.com, 111-222-3333 3. Roger Whitman, rwhitman@coding.com, 987-123-4567 4. Suzy Seashell,seashore@sell.com,444-444-4444

COMMAND MENU view - View a contact add - Add a contact del - Delete a contact exit - Exit program

Command: add Name: Tilly May Email: tilly@help.com Phone: 321-654-0987 Tilly May was added.

COMMAND MENU view - View a contact add - Add a contact del - Delete a contact exit - Exit program

Command: view 1. Guido van Rossum, guido@python.org, 123-456-7890 2. Eric Idle, eric@ericidle.com, 111-222-3333 3. Roger Whitman, rwhitman@coding.com, 987-123-4567 4. Suzy Seashell,seashore@sell.com,444-444-4444 5. Tilly May,tilly@help.com,321-654-0987

COMMAND MENU view - View a contact add - Add a contact del - Delete a contact exit - Exit program

Command: del 1. Guido van Rossum, guido@python.org, 123-456-7890 2. Eric Idle, eric@ericidle.com, 111-222-3333 3. Roger Whitman, rwhitman@coding.com, 987-123-4567 4. Suzy Seashell,seashore@sell.com,444-444-4444 5. Tilly,tilly@help.com,321654-0987 Enter the number of the contact to delete: 5 5 was removed

COMMAND MENU view - View a contact add - Add a contact del - Delete a contact exit - Exit program

Command: view 1. Guido van Rossum, guido@python.org, 123-456-7890 2. Eric Idle, eric@ericidle.com, 111-222-3333 3. Roger Whitman, rwhitman@coding.com, 987-123-4567 4. Suzy Seashell,seashore@sell.com,444-444-4444

COMMAND MENU view - View a contact add - Add a contact del - Delete a contact exit - Exit program

Command: exit Bye!

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 Databases Questions!