I wrote a phone book program with python and the program has the ability to add, delete, view contacts, search, load contacts saved in the file and when you add a contact, the contact is saved in a .txt file
The problem is that when a duplicate contact is entered, the overwrite function is executed, which is responsible for overwriting the file. It does the job correctly, but when you later delete the contact and enter the same again, the overwrite function is executed and says that the contact is saved in the file.
def over_write(name,new_number):
search_text = find_number("phone_book.txt",name).strip()
replace_text = f"'{name}' : '{new_number}'"
with open('phone_book.txt', 'r') as file:
data = file.read()
data = data.replace(search_text, replace_text)
with open('phone_book.txt', 'w') as file:
file.write(data)
print("Contact replaced")
I tried AI and didn’t work, and I change and debug the over_write
function but it didn’t work.
Havi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1