Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
In this exercise, you will create a menudriven 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 displaymenu 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. Use this variable containing the file name in your "open statement.
Take note that this code for opening the file is just to validate that the filename typed by the user actually exists before going on with the rest of the program. You will not actually use the file inside the "main function.
To check the file's existence you'll need to use a "tryexcept statement block when you attempt to open the file. Utilize the "FileNotFoundError" type in your except section and be sure to print a message if this occurs.
Place this entire section of the code input prompt, tryexcept etc. inside a "While" loop so that if an error occurs your code repeatedly ask the user for a new filename input. When the file opens successfully in the "try" portion then you should be sure to close the file and then "break" the loop.
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 Ifelifelifelifelse 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 "GoodBye". 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 # above as an argument.
The viewcontactscontactfile 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 addcontactscontactfile 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 deletecontactscontactfile 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 eg 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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started