-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
70 lines (48 loc) · 1.96 KB
/
Copy pathchatbot.py
File metadata and controls
70 lines (48 loc) · 1.96 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
import random
# lists of possible replies for variety
greetings = ["Hi there!", "Hello!", "Hey, good to see you!"]
how_are_you_replies = ["I'm doing great, thanks!", "I'm fine, how about you?", "Pretty good!"]
jokes = [
"Why do programmers prefer dark mode? Because light attracts bugs.",
"Why did the computer go to therapy? It had too many bytes of trauma.",
"I would tell you a UDP joke, but you might not get it."
]
unknown_replies = ["Sorry, I didn't get that.", "Can you rephrase that?", "I'm not sure what you mean."]
def get_response(msg, user_name):
msg = msg.lower()
if "hello" in msg or "hi" in msg:
return random.choice(greetings)
elif "how are you" in msg:
return random.choice(how_are_you_replies)
elif "my name is" in msg:
name = msg.split("my name is")[1].strip()
return "Nice to meet you, " + name.capitalize() + "!"
elif "your name" in msg:
return "I am a simple chatbot made in Python."
elif "joke" in msg:
return random.choice(jokes)
elif "thank" in msg:
return "You're welcome!"
elif "help" in msg:
return "You can say things like: hello, how are you, tell me a joke, my name is ___, or bye."
elif "bye" in msg:
if user_name != "":
return "Goodbye, " + user_name.capitalize() + "! Take care."
else:
return "Goodbye! Take care."
else:
return random.choice(unknown_replies)
def main():
print("Chatbot: Hi! I'm a simple chatbot. Type 'bye' anytime to end the chat.")
print("Chatbot: Type 'help' if you want to know what I can do.\n")
user_name = ""
while True:
msg = input("You: ")
# save name if the user introduces themselves
if "my name is" in msg.lower():
user_name = msg.lower().split("my name is")[1].strip()
response = get_response(msg, user_name)
print("Chatbot:", response)
if "bye" in msg.lower():
break
main()