In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of January 18th
(this exercise will still be available for submission after that deadline, but without counting towards your grade)
[to understand the context of this problem, you should read the class #12 exercise sheet]
In this problem you should submit the functions as described. Inside the functions do not print anything that was not asked!
Coding skills always come in handy, especially to manage your list of contacts!
Write a program that helps you manage your contact list stored in a text file. Your program should include the following functionality:
name
under the format {first_name last_name, phone}
, i.e., "Jane Doe, 123456789"
to the filename
. This function should additionally output "Contact {name} has been added."
For simplicity, assume that contacts with the same name and phone can be added to the contacts list (it is not required to verify and prevent this). If the file does not exist, then this function should create it and write the contact to the file, otherwise it simply appends the contact to the file (Hint: check which "mode" does this for you automatically, reviewing lecture T21: Files).filename
and displays all contacts, one per line. In case the file is empty, this function should print "No contacts found."
. You should also handle the case where the file does not exist by displaying the appropriate error message: use a try-except
block that handles the FileNotFoundError
and prints "Error: File Not Found!"
. This function should additionally output "All Contacts:"
followed by a contact per line in the format {name}, {phone}
.filename
and removes the contact with the matching name
. If the contact is not found, this function should print "Contact {name} not found."
. Again, if the file does not exist, print "Error: File Not Found!"
. If the operation is successful, the function should additionally output "Contact {name} has been deleted."
.name
will be deleted, even if their phone numbers are different."Jane Doe, 123456789"
and "Jane Doe, 987654321"
would both be deleted when callingdelete_contact("contacts.txt", "Jane Doe")
even though phone numbers are different.The following constraints are guaranteed in all the test cases that will be given to your program:
name
is always given as a string in the format {first_name last_name, phone}
, e.g., "Jane Doe"
, "Miriam Santos"
, "Pedro Ribeiro"
phone
is always given as a string with 9 digits, e.g., "123456789"
, "987654321"
filename
can be tested for files that do not existThe example inputs uses the filename contacts.txt
.
To make it easier for you to replicate the examples given below, add the following snippet of code in the beginning of your python file:
import os
if os.path.exists("contacts.txt"): os.remove("contacts.txt")
This ensures that, if a file named contacts.txt
exists in your current directory, it is deleted.
In this way, you guarantee that at the beginning of each execution, there is no file: it will be created and manipulated *independently* between runs.
Example Input 1 | Example Output 1 |
view_contacts("contacts.txt") |
Error: File Not Found! |
Example Input 2 | Example Output 2 |
delete_contact("contacts.txt", "Miriam Santos") |
Error: File Not Found! |
Example Input 3 | Example Output 3 |
view_contacts("contacts.txt") add_contact("contacts.txt", "Miriam Santos", "123456789") add_contact("contacts.txt", "Pedro Ribeiro", "987654321") view_contacts("contacts.txt") |
Error: File Not Found! Contact Miriam Santos has been added. Contact Pedro Ribeiro has been added. All Contacts: Miriam Santos, 123456789 Pedro Ribeiro, 987654321 |
Example Input 4 | Example Output 4 |
view_contacts("contacts.txt") add_contact("contacts.txt", "Miriam Santos", "123456789") add_contact("contacts.txt", "Pedro Ribeiro", "987654321") view_contacts("contacts.txt") delete_contact("contacts.txt", "Mateus Pereira") view_contacts("contacts.txt") |
Error: File Not Found! Contact Miriam Santos has been added. Contact Pedro Ribeiro has been added. All Contacts: Miriam Santos, 123456789 Pedro Ribeiro, 987654321 Contact Mateus Pereira not found. All Contacts: Miriam Santos, 123456789 Pedro Ribeiro, 987654321 |
Example Input 5 | Example Output 5 |
view_contacts("contacts.txt") add_contact("contacts.txt", "Miriam Santos", "123456789") add_contact("contacts.txt", "Pedro Ribeiro", "987654321") view_contacts("contacts.txt") delete_contact("contacts.txt", "Miriam Santos") view_contacts("contacts.txt") |
Error: File Not Found! Contact Miriam Santos has been added. Contact Pedro Ribeiro has been added. All Contacts: Miriam Santos, 123456789 Pedro Ribeiro, 987654321 Contact Miriam Santos has been deleted. All Contacts: Pedro Ribeiro, 987654321 |
Example Input 6 | Example Output 6 |
add_contact("contacts.txt", "Miriam Santos", "123456789") view_contacts("contacts.txt") |
Contact Miriam Santos has been added. All Contacts: Miriam Santos, 123456789 |
Example Input 7 | Example Output 7 |
add_contact("contacts.txt", "Miriam Santos", "123456789") |
Contact Miriam Santos has been added. |
Example Input 8 | Example Output 8 |
# Add 1 contact add_contact("contacts.txt", "Miriam Santos", "123456789") # Delete contact, file is now empty delete_contact("contacts.txt", "Miriam Santos") view_contacts("contacts.txt") |
Contact Miriam Santos has been added. Contact Miriam Santos has been deleted. No contacts found. |
Example Input 9 | Example Output 9 |
add_contact("contacts.txt", "Miriam Santos", "123456789") add_contact("contacts.txt", "Miriam Santos", "123456789") add_contact("contacts.txt", "Miriam Santos", "123456789") add_contact("contacts.txt", "Pedro Ribeiro", "987654321") view_contacts("contacts.txt") |
Contact Miriam Santos has been added. Contact Miriam Santos has been added. Contact Miriam Santos has been added. Contact Pedro Ribeiro has been added. All Contacts: Miriam Santos, 123456789 Miriam Santos, 123456789 Miriam Santos, 123456789 Pedro Ribeiro, 987654321 |
Example Input 10 | Example Output 10 |
add_contact("contacts.txt", "Miriam Santos", "123456789") add_contact("contacts.txt", "Miriam Santos", "123456789") add_contact("contacts.txt", "Miriam Santos", "123456789") add_contact("contacts.txt", "Pedro Ribeiro", "987654321") view_contacts("contacts.txt") delete_contact("contacts.txt", "Miriam Santos") view_contacts("contacts.txt") |
Contact Miriam Santos has been added. Contact Miriam Santos has been added. Contact Miriam Santos has been added. Contact Pedro Ribeiro has been added. All Contacts: Miriam Santos, 123456789 Miriam Santos, 123456789 Miriam Santos, 123456789 Pedro Ribeiro, 987654321 Contact Miriam Santos has been deleted. All Contacts: Pedro Ribeiro, 987654321 |