Python实现成语接龙

如图:

详细代码实现:

python 复制代码
# coding:utf-8
import string
import pypinyin
import sys
import random

print("初始化中,请稍等......")


def main():
    f2 = open('./idiom.txt', 'r', encoding='utf-8')
    f = f2.read()  # 一次性读取完成

    new3_list = f.split("\n")

    robot = ["哀鸿遍野", "言外之意", "口干舌燥", "地久天长", "不期而遇", "坐享其成", "知书达理", "遮天蔽日", "张灯结彩",
             "再接再厉", "月明星稀", "缘木求鱼", "永无止境", "引人入胜", "易如反掌", "烟消云散", "形影不离", "相依为命",
             "万马齐喑", "脱口而出", "天下太平", "手下留情", "杀气腾腾", "日月如梭", "全力以赴", "倾盆大雨", "奇形怪状",
             "朴实无华", "呕心沥血", "名不虚传", "眉开眼笑", "马到成功", "良苦用心", "聚沙成塔", "尽心尽力", "豁然开朗",
             "和颜悦色", "骨肉相连", "恶语伤人", "得意门生", "大惊小怪", "层次分明", "半死不活"]

    for i in new3_list:
        randomvar = random.randint(0, 1)
        if randomvar == 0:
            robot.append(i)

    robotpy1 = []
    for k in robot:
        robotpy1.append(pypinyin.pinyin(k, style=pypinyin.NORMAL)[0])

    py2_l = []

    py2 = ""
    for i2 in new3_list:
        for i in pypinyin.pinyin(str(i2), style=pypinyin.NORMAL):
            i = str(i).replace("[", '')
            i = str(i).replace("]", '')
            i = str(i).replace("'", '')
            py2 = py2 + str(i)
        py2_l.append(py2)

    def ifcy():
        for i in new3_list:
            if i == da:
                return True
        return False

    upcy = ' '
    while True:
        js = False
        da = input("你:")
        py3 = pypinyin.pinyin(da, style=pypinyin.NORMAL)[0]
        py6 = pypinyin.pinyin(da, style=pypinyin.NORMAL)[len(pypinyin.pinyin(da, style=pypinyin.NORMAL)) - 1]
        upcy1 = pypinyin.pinyin(upcy, style=pypinyin.NORMAL)[len(pypinyin.pinyin(upcy, style=pypinyin.NORMAL)) - 1]
        py = ""
        for i in pypinyin.pinyin(da, style=pypinyin.NORMAL):
            i = str(i).replace("[", '')
            i = str(i).replace("]", '')
            i = str(i).replace("'", '')
            py = py + str(i)

        if ifcy():
            if not upcy == " ":
                if py3 == upcy1:
                    print("你接上了!!!")
                else:
                    print("你没有接上,所以输了!!!")
                    input("按回车退出!!!")
                    sys.exit()
            l = 0
            for py5 in robotpy1:
                if py5 == py6:
                    print("小土豆:" + robot[l])
                    upcy = robot[l]
                    js = True
                    break
                l = l + 1

        else:
            print("不是成语!!!")
            input("按回车退出!!!")
            sys.exit()

        if not js:
            print("我输了,不过我会学习的!!!")
            input("按回车退出!!!")
            sys.exit()


def trydef(max_attempts=3):  # 设置一个最大尝试次数
    if max_attempts == 0:
        print("达到最大尝试次数,退出程序。")
        return
    try:
        main()
    except Exception as e:
        print(f"发生异常:{e}")
        trydef(max_attempts-1)  # 减少尝试次数

trydef()

关注私信我!发送idiom.txt文件!!!

相关推荐
Swift社区3 小时前
在 Swift 中实现字符串分割问题:以字典中的单词构造句子
开发语言·ios·swift
没头脑的ht3 小时前
Swift内存访问冲突
开发语言·ios·swift
没头脑的ht3 小时前
Swift闭包的本质
开发语言·ios·swift
wjs20243 小时前
Swift 数组
开发语言
stm 学习ing4 小时前
FPGA 第十讲 避免latch的产生
c语言·开发语言·单片机·嵌入式硬件·fpga开发·fpga
湫ccc5 小时前
《Python基础》之字符串格式化输出
开发语言·python
mqiqe6 小时前
Python MySQL通过Binlog 获取变更记录 恢复数据
开发语言·python·mysql
AttackingLin6 小时前
2024强网杯--babyheap house of apple2解法
linux·开发语言·python