Address book
Guys, hi. I have a problem. I made a class which help me to create contact for the address book. I've successfully made __init__ function , but I can't make remover function. I need to delete the line with the contact name and 3 subsequent lines. That's the problem. class Contact: library = 'address.txt' def __init__(self, name, number, description=''): self.name = name self.number = number self.description = description with open(Contact.library, 'a') as f: f.write(f'Имя: {self.name}\nНомер: {self.number}\nОписание: {self.description}\n\n') #name, number, description print('Контакт успешно создан*\n') #the contact was made successfully def del_contact(): import re with open(Contact.library) as f: list_of_strings = list(f) lines = f.readlines() #problem area!!! ''' deleting_contact = str(input('Введите имя удаляемого контакта: ')) #A name of delete contact pattern = re.compile(re.escape(deleting_contact)) with open(Contact.library, 'w') as f: for num, line in enumerate(lines, 1): deleting_strings = [ pattern.search(), list_of_strings[num], list_of_strings[num+1], list_of_strings[num+2] ] if line not in deleting_strings: f.write(n) ''' #problem area!!! print('Контакт успешно удалён\n') def show_all(): with open(Contact.library) as f: lines = f.readlines() for line in lines: print(line) #examples Contact('Andrew', '88005553535', '') Contact('John', '2281717') Contact.del_contact()