日常pytho3练习脚本之--两个逗比聊天机器人

点击蓝字,关注我们

这里首先感谢一下波嫂!

波嫂也非常忙天天加班到晚上8点半以后才回家,不过依然不辞辛苦的收集资料、头悬梁、锥刺股的学习如何设计文章样式。这是什么精神?这是无产阶级无私奉献的精神!波嫂犹如一把星星之火永远照亮着波哥前进的方向。

那么有了波嫂的支持呢,我们就应该做出更多更好的内容了。所以今天波哥用几十行代码写了两个聊天机器人。之前都是人机交互,那么两个机器人相互聊天会是什么效果呢?

请看下方:

是不是有点意思呀!哈哈哈哈!

仓库位置:

https://github.com/luckman666/mychat.git

由于代码比较少波哥就直接写在这里了:

import randomfrom time import sleepimport requestsdef GenerateWord():    s=''    for num in range(5):        head = random.randint(0xb0, 0xf7)        body = random.randint(0xa1, 0xf9)        val = f'{head:x}{body:x}'        s += bytes.fromhex(val).decode('gb2312')    return sdef Checkrepetition(chat,chatList):    if len(chatList) > 5:        chatList.pop(0)    if chat in chatList:        myword = GenerateWord()        return myword,chatList    else:        chatList.append(chat)        return chat,chatListdef Chitchat():    zs = input("请主人输入话题:")    chatList=[]    while True:        resp = requests.get("http://api.qingyunke.com/api.php", {'key': 'free', 'appid':0, 'msg': zs})        resp.encoding = 'utf8'        resp = resp.json()        sleep(1)        print('赵四:', resp['content'])        ln,chatList=Checkrepetition(resp['content'],chatList)        resp = requests.get("http://api.qingyunke.com/api.php", {'key': 'free', 'appid':0, 'msg': ln})        resp.encoding = 'utf8'        resp = resp.json()        sleep(1)        print('刘能:', resp['content'])        zs,chatList=Checkrepetition(resp['content'], chatList)if __name__ == "__main__":    Chitchat()

简单介绍一下这个脚本:

这里为了使两个小家伙聊的开心起劲呢。

1、波哥写了一个效验函数(Checkrepetition)来判断其是否重复。

2、如果有重复呢就随机生成五个汉字塞给机器人(GenerateWord)

然后就会出现很多意外的回复。。可能会很污的哦(未成年人禁用)!

记得帮波哥转发哦!!

PS:这几天想着要给大家录一期视频,结果不是麦克买错了就是转接头不好使。。真是醉了!

扫码关注,回复“全栈资料”会有

意想不到的收获哦

你“在看”我吗?

猜你喜欢

转载自www.cnblogs.com/devops-ITboge/p/11463185.html