-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (60 loc) · 1.9 KB
/
main.py
File metadata and controls
73 lines (60 loc) · 1.9 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
phone_base = {}
def input_error(func):
def inner(*args):
try:
return func(*args)
except IndexError:
return "Not enough parametrs. Please, enter Command, Name and Phone number"
except KeyError:
return "Wrong Name. Please enter correct Name"
return inner
@input_error
def hello():
return "How I can help you?"
@input_error
def add_record(*args):
user_name = args[0]
phone_number = args[1]
phone_base[user_name] = phone_number
return f"Added user: {user_name} with phone number: {phone_number}"
@input_error
def change_record(*args):
user_name = args[0]
new_phone_number = args[1]
new_record = phone_base[user_name]
if new_record:
phone_base[user_name] = new_phone_number
return f"Users {user_name} phone changed to {new_phone_number}"
@input_error
def show_phone(*args):
user_name = args[0]
return f"{user_name} phone number {phone_base[user_name]}"
@input_error
def show_all(*args):
return phone_base
@input_error
def good_bye(*args):
return "\nGood bye."
COMMANDS = {hello: "hello",
add_record: "add",
change_record: "change",
show_phone: "phone",
show_all: "show all",
good_bye: ("good bye", "close")}
def unknown_command(*args):
return "Unknown command. Please try again."
def parser(text: str):
for func, kw in COMMANDS.items():
register_text = text.lower()
if register_text.startswith(kw):
return func, text[len(kw):].strip().split()
return unknown_command, []
def main():
while True:
input_command = input("Please input command: ")
func, data = parser(input_command)
print(func(*data))
if func(*data) == "\nGood bye.":
break
if __name__ == "__main__":
main()