forked from hezida/python-excercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonebook.py
68 lines (52 loc) · 1.83 KB
/
phonebook.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import sys
import os.path
book = dict()
FILE_PATH = 'C:\\python\\MyPhonebook.txt'
def create():
first_name = input('Enter first name: ')
phone_number = input('Enter Phone Number: ')
book[first_name] = phone_number
print("Your record was successfully saved")
def read():
name = input('Enter the name of the record you want to get: ')
if name in book:
phone = book[name]
print('The phone number of {} is {}'.format(name, phone))
else:
print("record was not found")
def update():
name = input('Enter the record name you want to get: ')
if name not in book:
print("record was not found")
return
phone = input('Enter the new phone number of {}: '.format(name))
book[name] = phone
def delete():
name = input('Enter the name of the record you want to delete: ')
if name not in book:
print("record was not found")
return
del book[name]
def leave():
with open(FILE_PATH, 'w') as f:
for name, phone in book.items():
f.write('{}\t{}'.format(name, phone))
sys.exit()
def main():
file_exists = os.path.isfile(FILE_PATH)
if file_exists:
print('file exists')
with open(FILE_PATH, 'r') as f:
for line in f:
name, phone = line.split('\t')
book[name] = phone
while True:
print('Select one of your choice: \n'
'-------------------------- \n'
'c - Create a new phone record \n'
'r - Read a phone record by name \n'
'u - Update an existing phone record \n'
'd - Delete an existing phone record \n')
menu_choices = {'c': create, 'r': read, 'u': update, 'd': delete, 'q': leave}
menu_choices[input()]()
main()