-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (53 loc) · 2.38 KB
/
main.py
File metadata and controls
67 lines (53 loc) · 2.38 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
def parse_input(user_input):
cmd, *args = user_input.split()
cmd = cmd.strip().lower()
return cmd, *args
def add_contact(args, contacts):
name, phone = args
contacts[name] = phone
return "Contact added."
def change_contact(args, contacts):
name, phone = args
if name in contacts:
contacts[name]= phone
return "Contact updated."
else:
return "Error: no contact."
def show_phone(name, contacts):
if name in contacts:
return contacts.get(name)
else:
return "Error: no contact."
def show_all(contacts):
# return "\n".join(contacts)
return "\n".join([f"{name}: {phone}" for name, phone in contacts.items() if phone is not None])
# all_contacts = ""
# for name, phone in contacts.items():
# all_contacts += f"{name}: {phone}\n"
# return all_contacts
def main():
contacts = {}
print("Welcome to the assistant bot!")
while True:
user_input = input("Enter a command: ")
command, *args = parse_input(user_input)
if command in ["close", "exit"]: # "close" або "exit"
print("Good bye!") # output_text: "Good bye!"
break
# return
# main.exit() # Полностью завершаем программу import sys
elif command == "hello": # "hello"
print("How can I help you?") # output_text: "How can I help you?"
elif command == "add": # "add [ім'я] [номер телефону]" "add John 1234567890"
print(add_contact(args, contacts)) # output_text: "Contact added."
elif command == "change": # "change [ім'я] [новий номер телефону]" "change John 0987654321"
print(change_contact(args, contacts)) # output_text: "Contact updated."
elif command == "phone": # "phone John"
name = args[0]
print(show_phone(name, contacts)) # output: [номер телефону]
elif command == "all": # "all"
print(show_all(contacts)) # output: усі збережені контакти
else:
print("Invalid command.")
if __name__ == "__main__":
main()