吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4858|回复: 24
收起左侧

[Python 转载] 刚学python弄的成语接龙

[复制链接]
老干妈捞奶茶 发表于 2020-4-10 20:54
本帖最后由 老干妈捞奶茶 于 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[0]] = row[3:]

idiom = random.choice(list(dic))
explain = dic[idiom]
print('开始接龙:' + idiom)
print('【解释】' + explain[0])
print('【出处】' + explain[1])

n = 0
already = [idiom]
while True:
    n += 1
    number = '开始第{}轮游戏!'.format(n)
    print(number)
    while True:
        ex = explain
        begin = input('')
        if begin in list(dic.keys()):
            if dic[begin][-2] != ex[-1]:
                conformity = '你输入的【{}】这个成语不符合接龙'.format(begin)
                print(conformity)
                continue
            elif begin in already:
                repetition = '接龙成语不能重复哦!'
                print(repetition)
                continue
            elif dic[begin][-2] == ex[-1]:
                expound = '【解释】' + dic[begin][0] + '\n'
                source = '【出处】' + dic[begin][1] + '\n'
                success = '【{}】接龙成功'.format(begin) + '\n'
                print(expound + source + success)
                already.append(begin)
                explain = dic[begin]
                break
        if begin == '不会':
            answer = list(dic.keys())[[i[-2] for i in list(dic.values())].index(dic[already[n-1]][-1])]
            over = '此轮结束,答案【{}】'.format(answer) + '\n'
            expound = '【解释】' + dic[answer][0] + '\n'
            source = '【出处】' + dic[answer][1] + '\n'
            print(over + expound + source)
            already.append(answer)
            explain = dic[answer]
            break
        if begin not in list(dic.keys()):
            nothingness = '【{}】这不是成语哦!'.format(begin)
            print(nothingness)
            continue
    if n == 10:
        print('游戏结束')
        break

成语接龙规则:随机抽取词库成语,直到接满10条则游戏结束,可以同音字接龙。

可惜就差最后一步,研究了很久都不会用itchat,然后就放弃了{:1_908:}
网上的那些图灵机器人什么的听说要钱,我也没去了解了。
附上成语词库链接
[md]```

成语大全.txt

42 Bytes, 下载次数: 61, 下载积分: 吾爱币 -1 CB

成语大全链接

免费评分

参与人数 4吾爱币 +4 热心值 +3 收起 理由
xiaofrank + 1 + 1 热心回复!
z208722006 + 1 我很赞同!可以哈哈
loner. + 1 + 1 用心讨论,共获提升!
冷诗烟 + 1 + 1 很有趣

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

每年工资两千五 发表于 2020-11-12 18:02
[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/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[0]] = row[3:]
 
word = random.choice(list(words))
print('开始接龙:' + word)
print('【解释】' + words[word][0])
print('【出处】' + words[word][1])
print('【拼音】' + words[word][2])
print('【首字音】' + words[word][3])
print('【尾字音】' + words[word][4])
 
ing = 0  # 当前关数
end = 2  # 总关数
tip = 13  # 提示的次数
already = [word]  # 已经回答过的成语
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[u_word][3] != words[word][-1]:
                print('你输入的【{}】这个成语不符合接龙,你需要接龙的首字拼音为【{}】'.format(u_word, words[already[word]][4]))
                continue
            elif u_word in already:
                print('接龙成语不能重复哦!你需要接龙的首字拼音为【{}】'.format(words[already[word]][4]))
                continue
            elif words[u_word][3] == words[word][-1]:
                expound = '【解释】' + words[u_word][0] + '\n'
                source = '【出处】' + words[u_word][1] + '\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())[[i[3] for i in list(words.values())].index(words[word][-1])]
            over = '此轮结束,答案【{}】'.format(answer) + '\n'
            expound = '【解释】' + words[answer][0] + '\n'
            source = '【出处】' + words[answer][1] + '\n'
            print(over + expound + source)
            already.append(answer)
            word = words[answer]
            quit = 0
            break
        if u_word == "提示" and tip > 0:
            tip -= 1
            answer = list(words.keys())[[i[3] for i in list(words.values())].index(words[word][-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[word][4]))
            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 21:53
本帖最后由 老干妈捞奶茶 于 2020-4-10 22:15 编辑

成品效果图
如果可以的话,希望有大佬能把接到微信的代码写出来

123

123
冷诗烟 发表于 2020-4-10 22:21
 楼主| 老干妈捞奶茶 发表于 2020-4-10 22:26
本帖最后由 老干妈捞奶茶 于 2020-4-10 22:27 编辑

我设置了不能重复接龙,这个接龙的规则可以同音字接龙哈哈哈
123.png
冷诗烟 发表于 2020-4-10 22:32
哈哈,既然这样你的代码我拷走了
jidesheng6 发表于 2020-4-10 22:53
不要用itchat了,我自己有个微信机器人,但是代码写的不咋样,用wxpy做的,地址:https://github.com/jidesheng6/WechatBotBaseWxpy-
你可以借鉴一下看看有没有灵感
 楼主| 老干妈捞奶茶 发表于 2020-4-10 23:19
jidesheng6 发表于 2020-4-10 22:53
不要用itchat了,我自己有个微信机器人,但是代码写的不咋样,用wxpy做的,地址:https://github.com/jides ...

好,谢谢大佬哈
天黑我隐身 发表于 2020-4-10 23:26
wxpy是一个基于itchat封装的第三方库,对用户更友好,官方文档写的很棒,大人小孩都馋哭了(一条五毛括号删除
shitouhn 发表于 2020-4-10 23:51
辛苦了,支持一下
jcf 发表于 2020-4-11 00:02
支持一下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-13 14:21

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表