In [3]:
import random
def win():
print('''
======恭喜你,你赢了=======
."". ."",
| | / /
| | / /
| | / /
| |/ ;-._
} ` _/ / ;
| /` ) / /
| / /_/\_/\
|/ / |
( ' \ '- |
\ `. /
| |
| |
======恭喜你,你赢了=======
''')
def lose():
print('''
======YOU LOSE=======
.-" "-.
/ \
| |
|, .-. .-. ,|
| )(__/ \__)( |
|/ /\ \|
(@_ (_ ^^ _)
_ ) \_______\__|IIIIII|__/__________________________
(_)@8@8{}<________|-\IIIIII/-|___________________________>
)_/ \ /
(@ `--------`
======YOU LOSE=======
''')
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 guess_poem_game(file_name): ##猜诗句游戏
with open(file_name) as fh:
text = fh.read()
poems = text.split()
poem = random.choice(poems) ##系统随机选择一句诗
chs = get_ch_table(text.replace('\n',''))
guess_ch_table = [ch for ch in poem]
while len(guess_ch_table)<9:
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,9,3):
print(guess_ch_table[i],guess_ch_table[i+1],guess_ch_table[i+2]) ##形成3x3列表
return poem
def main():
file_name = r'd:\temp\诗词.txt'
score = 10
while score>=0:
real_poem = guess_poem_game(file_name)
answer_poem = input('请输入猜测的诗句,回车结束,直接回车表示退出')
if answer_poem == real_poem:
score+=10
print('答对了,加十分!')
print('您当前的分数是:',score)
if score == 100:
win()
return
elif answer_poem == '':
print('退出游戏')
print('您最后得分为:',score)
return
else:
score -= 10
print('答错了,扣十分!')
print('真实答案是:',real_poem)
print('您当前的分数是:',score)
else:
lose()
return
main()