In [3]:
import random
def welcome():
    print('Welcome to the game.')
def win():
    print('Congratulation !you win!')
def lose():
    print('Sorry you lose. ')
def gameover():
    print('GAMEOVER, Goodbye!')  
def get_idioms (filename):
    fh=open(filename)
    text=fh.read()
    fh.close()
    idioms=text.split()
    return idioms
def get_chtable(filename):
    fh=open(filename)
    text=fh.read()
    text.replace('\n','')
    ch_table=[]
    for ch in text :
        if ch not in ch_table:
            ch_table.append(ch)
    return ch_table
def get_gusstable(idiom,ch_table):
    guss_table=[ch for ch in idiom]
    while len(guss_table)<6:
        ch=random.choice(ch_table)
        if ch not in guss_table:
            guss_table.append(ch)
    random.shuffle(guss_table)
    for i in range(0,6,2):
        print(guss_table[i],guss_table[i+1],sep='   ')
def play_gussidiom():
    filename=r'd:/temp/idioms_correct.txt'
    idioms=get_idioms(filename)
    ch_table=get_chtable(filename)
    score=10
    welcome()
    print('你的初始分数是:',score,'分')
    while score>0:
        idiom=random.choice(idioms)
        get_gusstable(idiom,ch_table)
        your_guss=input('请输入你猜的成语: ')
        if your_guss==idiom :
            score+=10
            if score==100:
                print('你当前的分数是:',score,'分')
                win()
                break
            print('你猜对了!你当前的分数是:',score,'分')
        elif your_guss=='':
            break
        else:
            score-=10
            print('你猜错了!你当前的分数是:',score,'分')
    if score==0:
        lose()
        print('正确的成语是:',idiom)
    gameover()


play_gussidiom()


Welcome to the game.
你的初始分数是: 10 分
肉   伦
与   讳
无   比
请输入你猜的成语: 无肉伦比
你猜错了!你当前的分数是: 0 分
Sorry you lose. 
正确的成语是: 无与伦比
GAMEOVER, Goodbye!

In [ ]:
import random
filename=r'd:/temp/words.txt'
def get_words(filename): 
    with open(filename) as fh:
        text=fh.read()
    text.replace(' ','')
    words=text.split()
    return words
def get_key(words):
    key=[]
    for word in words:
        n=len(word)
        m=random.randrange(10**8,10**9)
        key.append(str(n)+str(m))
    return key
def change_word(xkey,xword):
    i=0
    word=[]
    for ch in xword :
        n=xkey[i]
        if ch>'Z':
            m=ord(ch)+int(n)
            if m>122:
                m=m%122+97
        else:
            m=ord(ch)+int(n)
            if m>90:
                m=m%90+65
        word.append(chr(m))
        i+=1
    while len(word)<10:
        num=random.randrange(97,112)
        word.append(chr(num))
    return word

filename=r'd:/temp/words.txt'
words=get_words(filename)
key=get_key(words)
new_words=[]
i=0
for xword in words:
    xkey=key[i]
    new_word=change_word(xkey,xword)
    new_words.append(new_word)
    i+=1
fh=open(r'd:/temp/jiami.txt','w')
for word in new_words:
    fh.writelines(word)
    fh.write(' ')
fh.close()

def recover_word(xword,xkey):
    n=int(xkey[0])
    origin_word=[]
    for i in range(n):
        m=ord(xword[i])
        k=int(xkey[i])
        if xword[i]<'Z':
            if m-k<65:
                m=m+25-k
            else:
                m-=k
        else:
            if m-k<97:
                m=m+25-k
            else:
                m-=k
        origin_word.append(chr(m))
    return origin_word

filename=r'd:/temp/jiami.txt'
words=get_words(filename)
key=get_key(words)
origin_words=[]
i=0
for xword in words:
    xkey=key[i]
    origin_word=recover_word(xword,xkey)
    print(origin_word)
    origin_words.append(origin_word)
    i+=1

fh=open(r'd:/temp/jiemi.txt','w')
for word in origin_words:
    fh.writelines(word)
    fh.write(' ')
    fh.close()