In [ ]:
import random
def win():
print(“you win!”)
return
def lose():
print(“you lose…”)
return
def get_ch_table(line):
ch_table=[]
for ch in line:
if ch not in ch_table:
ch_table.append(ch)
return ch_table
//打开文件并制作随机机器人
def idiom_robot(file_name):
with open(file_name) as fh:
text=fh.read()
idioms=text.split()
idiom=random.choice(idioms)
chs=get_ch_table(text.replace(r’\n’,’’))
guess_ch_table=[ch for ch in idiom]
while len(guess_ch_table)<12:
ch=random.choice(chs)
if ch not in guess_ch_table:
guess_ch_table.append(ch)
random.shuffle(guess_ch_table)
for i in range(0,12,3):
print(guess_ch_table[i],guess_ch_table[i+1],guess_ch_table[i+2])
return idiom
//function main
def main():
filename=r’d:\temp\idioms_correct.txt’
score=10
while score>=0:
real_idiom=idiom_robot(filename)
answer_idiom=input(‘请输入猜测的诗句,回车结束。直接回车结束游戏。’)
if answer_idiom==real_idiom:
print(‘答对了,加10分。’)
score+=10
print(‘您当前的分数为:’,score)
if score==100:
win()
return
elif answer_idiom==‘’
print(‘您最后的分数是:’,score,’\n’,’游戏结束。’)
return
else:
print(‘答错了,减10分。’)
score-=10
print(‘您当前的分数为:’,score)
else:
lose()
return
main()