class Phone_Book def new_contact self name surname tel self name name

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Phone_Book:
def new_contact(self, name, surname, tel):
self.name = name
self.surname = surname
self.tel = tel
print 'Person:', self.name, self.surname
print 'Telephone number: ', self.tel
def contact_write(self, contacts):
with open('address_book.txt', 'a') as address_book:
address_book.write('%s\n' % ' '.join(contacts))
print'Contact saved'
def show_book(self):
address_book = open('address_book.txt', 'r')
line = address_book.readline()
print('Show me address book:')
while line:
print(line)
line = address_book.readline()
address_book.close()
def search_name(self):
name = str(input('What we need to find: '))
with open('address_book.txt', 'r') as address_book:
lines = [line for line in address_book.readlines() if name in line]
if lines:
for line in lines:
print line
else:
print("Can't find")
if __name__ == '__main__':
new_contact = Phone_Book()
print('1 Add new contact \n'
'2 Search by name or number \n'
'3 Show all files \n')
choose = input('Choose what we do: ')
if choose == 1:
name = str(input('Name: '))
surname = str(input('Surname: '))
tel = str(input('Tel: '))
contacts = [name, surname, tel]
new_contact.new_contact(name, surname, tel)
new_contact.contact_write(contacts)
elif choose == 2:
new_contact.search_name()
elif choose == 3:
new_contact.show_book()