帮我用Python写一个智能聊天程序
时间: 2024-03-06 15:21:22 浏览: 41
不到20行代码用Python做一个智能聊天机器人
好的,下面是一个简单的智能聊天程序:
```python
import random
# 定义回答列表
greetings = ['Hello!', 'Hi!', 'Hey!', 'How are you?', 'Nice to meet you.']
goodbyes = ['Goodbye!', 'Bye!', 'See you later.']
questions = ['How old are you?', 'What is your name?', 'Where are you from?', 'What do you like to do?', 'What is your favorite color?']
answers = ['I am a chatbot.', 'My name is PythonBot.', 'I am from the internet.', 'I like to help people.', 'My favorite color is blue.']
# 定义回答函数
def respond(input_text):
if 'hello' in input_text.lower() or 'hi' in input_text.lower() or 'hey' in input_text.lower():
return random.choice(greetings)
elif 'bye' in input_text.lower() or 'goodbye' in input_text.lower():
return random.choice(goodbyes)
elif '?' in input_text:
return random.choice(answers)
else:
return "I'm sorry, I don't understand."
# 主程序
print("Welcome to PythonBot!")
while True:
input_text = input("You: ")
if input_text.lower() == 'quit':
print(random.choice(goodbyes))
break
response = respond(input_text)
print("PythonBot:", response)
```
这个程序可以回答一些简单的问题,也可以打招呼和告别。运行程序后,输入问题或者打招呼,程序会根据输入自动选择回答。如果想退出程序,输入“quit”即可。
阅读全文