如图:
详细代码实现:文章来源:https://www.toymoban.com/news/detail-708081.html
# 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文件!!!文章来源地址https://www.toymoban.com/news/detail-708081.html
到了这里,关于Python实现成语接龙的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!