刚学python弄的成语接龙
本帖最后由 老干妈捞奶茶 于 2020-4-10 21:50 编辑最近朋友的微信群弄了个机器人,有个游戏成语接龙我们玩得很过瘾,但是竟然每天限制次数,这很是令我扫兴。
然后我突发奇想能不能自己弄一个出来,接到微信去,然后写了一个下午
```
import itchat
import csv
import random
dic = {}
with open(r'成语大全.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
dic] = row
idiom = random.choice(list(dic))
explain = dic
print('开始接龙:' + idiom)
print('【解释】' + explain)
print('【出处】' + explain)
n = 0
already =
while True:
n += 1
number = '开始第{}轮游戏!'.format(n)
print(number)
while True:
ex = explain
begin = input('')
if begin in list(dic.keys()):
if dic[-2] != ex[-1]:
conformity = '你输入的【{}】这个成语不符合接龙'.format(begin)
print(conformity)
continue
elif begin in already:
repetition = '接龙成语不能重复哦!'
print(repetition)
continue
elif dic[-2] == ex[-1]:
expound = '【解释】' + dic + '\n'
source = '【出处】' + dic + '\n'
success = '【{}】接龙成功'.format(begin) + '\n'
print(expound + source + success)
already.append(begin)
explain = dic
break
if begin == '不会':
answer = list(dic.keys())[ for i in list(dic.values())].index(dic][-1])]
over = '此轮结束,答案【{}】'.format(answer) + '\n'
expound = '【解释】' + dic + '\n'
source = '【出处】' + dic + '\n'
print(over + expound + source)
already.append(answer)
explain = dic
break
if begin not in list(dic.keys()):
nothingness = '【{}】这不是成语哦!'.format(begin)
print(nothingness)
continue
if n == 10:
print('游戏结束')
break
```
成语接龙规则:随机抽取词库成语,直到接满10条则游戏结束,可以同音字接龙。
可惜就差最后一步,研究了很久都不会用itchat,然后就放弃了{:1_908:}
网上的那些图灵机器人什么的听说要钱,我也没去了解了。
附上成语词库链接```
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# import itchat
import csv
import random
words = {}
with open(r'world.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
words] = row
word = random.choice(list(words))
print('开始接龙:' + word)
print('【解释】' + words)
print('【出处】' + words)
print('【拼音】' + words)
print('【首字音】' + words)
print('【尾字音】' + words)
ing = 0# 当前关数
end = 2# 总关数
tip = 13# 提示的次数
already = # 已经回答过的成语
quit = 1
while quit:
ing += 1
print('开始第{}场游戏,总{}场'.format(ing, end))
while True:
u_word = raw_input('【{}】,输入你的成语:'.format(word))
if u_word in list(words.keys()):
if words != words[-1]:
print('你输入的【{}】这个成语不符合接龙,你需要接龙的首字拼音为【{}】'.format(u_word, words]))
continue
elif u_word in already:
print('接龙成语不能重复哦!你需要接龙的首字拼音为【{}】'.format(words]))
continue
elif words == words[-1]:
expound = '【解释】' + words + '\n'
source = '【出处】' + words + '\n'
success = '【{}】接龙成功'.format(u_word) + '\n'
print(expound + source + success)
already.append(u_word)
word = u_word
break
if u_word == '不会':
answer = list(words.keys())[ for i in list(words.values())].index(words[-1])]
over = '此轮结束,答案【{}】'.format(answer) + '\n'
expound = '【解释】' + words + '\n'
source = '【出处】' + words + '\n'
print(over + expound + source)
already.append(answer)
word = words
quit = 0
break
if u_word == "提示" and tip > 0:
tip -= 1
answer = list(words.keys())[ for i in list(words.values())].index(words[-1])]
print('提示:你可以答【{}】,你还剩下{}次提示机会'.format(answer, tip) + '\n')
continue
if u_word == "提示" and tip <= 0:
print('你没有提示机会哦!' + '\n')
continue
if u_word == "666":
tip += 10
print('恭喜你找到隐藏福利,提示机会+10次,你还有{}次提示'.format(tip) + '\n')
continue
if u_word not in list(words.keys()):
print('【{}】这不是成语哦!你需要接龙的首字拼音为【{}】'.format(u_word, words))
continue
if ing >= end:
print('游戏结束!获得10000W')
con = raw_input('是否继续游戏?(是/否):')
if con == "是":
word = random.choice(list(words.keys()))
ing = 0
end = 50
already = []
continue
else:
break
添加提示功能以及继续玩奖金就莫得了 本帖最后由 老干妈捞奶茶 于 2020-4-10 22:15 编辑
成品效果图
如果可以的话,希望有大佬能把接到微信的代码写出来{:1_932:} 为所谓为接龙 本帖最后由 老干妈捞奶茶 于 2020-4-10 22:27 编辑
冷诗烟 发表于 2020-4-10 22:21
为所谓为接龙
我设置了不能重复接龙,这个接龙的规则可以同音字接龙哈哈哈 哈哈,既然这样你的代码我拷走了{:301_971:} 不要用itchat了,我自己有个微信机器人,但是代码写的不咋样,用wxpy做的,地址:https://github.com/jidesheng6/WechatBotBaseWxpy-
你可以借鉴一下看看有没有灵感 jidesheng6 发表于 2020-4-10 22:53
不要用itchat了,我自己有个微信机器人,但是代码写的不咋样,用wxpy做的,地址:https://github.com/jides ...
好,谢谢大佬哈 wxpy是一个基于itchat封装的第三方库,对用户更友好,官方文档写的很棒,大人小孩都馋哭了(一条五毛括号删除 辛苦了,支持一下 支持一下