-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw_module_09.py
More file actions
98 lines (72 loc) · 2.25 KB
/
hw_module_09.py
File metadata and controls
98 lines (72 loc) · 2.25 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from functools import wraps
dict_of_contacts = {}
def input_error(func):
@wraps(func)
def inner(*args):
try:
return func(*args)
except IndexError:
return print("Give me name and phone please")
except ValueError:
return print("Enter user name")
except KeyError:
return print("Try again. Not correct key")
return inner
def hello(*args):
return print("How can I help You?")
@input_error
def add(*args):
list_of_param = args[0].split()
global dict_of_contacts
name = str(list_of_param[0])
number = list_of_param[1:]
if not number:
raise IndexError()
dict_of_contacts.update({name.capitalize(): list_of_param[1:]})
return dict_of_contacts
@input_error
def change(*args):
list_of_param = args[0].split()
global dict_of_contacts
name = str(list_of_param[0])
for keys in dict_of_contacts.keys():
if name.capitalize() == keys:
dict_of_contacts.update({keys: list_of_param[1:]})
number = list_of_param[1:]
if not number:
raise IndexError()
return f"{dict_of_contacts}"
@input_error
def phone(*args):
global dict_of_contacts
name = str(args[0])
for keys in dict_of_contacts.keys():
if name.capitalize() == keys:
return print('\n'.join(dict_of_contacts.get(keys)))
if not name:
raise ValueError()
def show_all(*args):
return print('\n'.join([f'{k}: {", ".join(v)}' for k, v in dict_of_contacts.items()]))
def exit(*args):
return print("Good bye!")
def no_command(*args):
return print("Unknown command, try again")
COMMANDS = {hello: "hello", add: "add", change: "change", phone: "phone",
show_all: "show all", exit: ["good bye", "close", "exit"]}
def handler(text):
for command, kword in COMMANDS.items():
if text in kword:
if type(kword) is str:
return command, text.replace(kword, "").strip()
else:
return command, None
return no_command, None
def main():
while True:
user_input = input(">>>")
command, data = handler(user_input.lower())
command(data)
if command == exit:
break
if __name__ == "__main__":
main()