python简单聊天机器人代码
时间: 2023-10-14 12:11:47 浏览: 47
可以参考以下代码:
```
import random
greetings = ["hello", "hi", "hey", "hola", "hey there", "greetings", "good morning", "good afternoon", "good evening"]
goodbyes = ["bye", "goodbye", "see you later", "ciao", "adios"]
questions = ["how are you", "what's up", "how's it going", "how's life", "how's everything"]
print("Hi, I'm a chatbot. What's your name?")
name = input()
print("Nice to meet you, " + name + ". How can I help you?")
while True:
user_input = input().lower()
if user_input in greetings:
bot_response = random.choice(greetings)
elif user_input in goodbyes:
bot_response = random.choice(goodbyes)
elif user_input in questions:
bot_response = "I'm doing well, thank you. How about you?"
else:
bot_response = "I'm sorry, I didn't understand what you said."
print(bot_response)
```
这是一个简单的聊天机器人代码,它可以回答一些问候、道别和常见问题。你可以根据自己的需求进行修改和扩展。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)