In [3]:
##完整的诗词游戏

import random

def win():
    print('win')
    return

def lose():
    print('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 poem_robot(file_name):
    with open(file_name) as fh:
        text = fh.read()
    poems = text.split()
    poem = random.choice(poems)
    chs = get_ch_table(text.replace(r'\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])
    
    return poem

def main():
    filename = r'/Users/pepper/Desktop/诗词.txt'
    score = 10
    while score >= 0:
        real_poem = poem_robot(filename)
        answer_poem = input('请输入猜测诗句:')
        if answwer_poem == real_poem:
            print('答对了,加十分')
            score += 10
            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

if __name__ == '__main()__':
    main()

In [ ]: