上一篇讲了小游戏的坑现在把源码放出来
#coding:utf-8
import string
import pypinyin
import sys
import random print("初始化中,请稍等……")
def main():
f2 = open('idiom.txt', 'r')
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():
try:
main()
except:
trydef() trydef()
提醒在同级目录下要放 idiom.txt(成语大全用来判断是不是成语,可能不全请自行添加!) 链接: https://pan.baidu.com/s/1gLWZYfvjgoCktyXnHOHa7g 提取码: nt4x