-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
83 lines (62 loc) · 1.97 KB
/
main.py
File metadata and controls
83 lines (62 loc) · 1.97 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
def input_error(func):
def inner(*args, **kwargs):
try:
result = func(*args, **kwargs)
return result
except KeyError:
return 'No user with this name'
except ValueError:
return 'Give me name and phone please'
except IndexError:
return 'Enter user name'
return inner
@input_error
def add_handler(*args, **kwargs):
name, phone = args[0].split(' ')
contacts[name] = phone
return 'Contact added'
@input_error
def exit_handler():
return "Good bye!"
@input_error
def hello_handler():
return 'Hello, how I can help you?'
@input_error
def change_handler(*args, **kwargs):
name, phone = args[0].split(' ')
if name not in contacts:
raise IndexError
contacts[name] = phone
return f"Phone number for {name} changed to {phone}"
@input_error
def phone_handler(name):
if name not in contacts:
raise IndexError
return f"Phone number for {name}: {contacts[name]}"
@input_error
def show_all_handler():
if not contacts:
return "No contacts available"
result ='\n'.join(f"{name}: {phone}" for name, phone in contacts.items())
return result
contacts={}
def main():
while True:
user_input = input("Enter command: ").lower()
if user_input in ["good bye", "close", "exit"]:
print(exit_handler())
break
elif user_input=='hello':
print(hello_handler())
elif user_input.startswith('add'):
print(add_handler(user_input[4:]))
elif user_input.startswith("change"):
print(change_handler(user_input[7:]))
elif user_input.startswith('phone'):
print(phone_handler(user_input[6:]))
elif user_input=="show all":
print(show_all_handler())
else:
print("Unknown command. Try again.")
if __name__ == '__main__':
main()