写一个qq机器人
import random
定义函数,实现自动回复功能
def auto_reply(msg): # 定义常见问题和对应的回答 common_questions = { "你好": "你好,有什么可以帮到你的?", "天气": "请告诉我你所在的城市,我可以帮你查询天气。", "笑话": "为什么脱衣舞女郎都穿这么少的衣服呢?因为衣服太少了,不够穿啊!", "你叫什么名字": "我是QQ机器人,很高兴认识你!" }
# 随机回答其他问题
other_reply = [
"抱歉,我不明白你的意思,请重新输入。",
"真的吗?",
"我会再努力学习的!",
"很高兴能和你聊天!",
"你可以问我一些有趣的问题哦!"
]
# 判断是否是常见问题
if msg in common_questions:
return common_questions[msg]
else:
return random.choice(other_reply)
主程序
print("你好,我是QQ机器人,请问有什么可以帮到你的?") while True: input_msg = input("请输入消息:") if input_msg == "退出": print("再见!") break else: reply = auto_reply(input_msg) print(reply
原文地址: http://www.cveoy.top/t/topic/hSBL 著作权归作者所有。请勿转载和采集!