In [1]:
def win():
print("win")
def lose():
print("lose")
In [2]:
def get_file():
with open("C:\\Users\\YSX-WDCZ\\Desktop\\idioms.txt",'r') as fh:
text = fh.read()
text = text.replace('、','')
idioms = text.split()
with open('C:\\Users\\YSX-WDCZ\\Desktop\\new_idioms.txt','w') as fn:
for idiom in idioms:
fn.write(idiom+'\n')
In [3]:
def get_words(str):
with open(str,'r') as fn:
words = fn.read().split()
ch_all = []
for word in words :
for ch in word :
if ch not in ch_all:
ch_all.append(ch)
return ch_all
In [4]:
import random
def word_choose(str,ch_all):
with open(str,'r') as fn:
words = fn.read().split()
word = random.choice(words)
ch_word = []
for ch in word :
if ch not in ch_word:
ch_word.append(ch)
i=0
while(i<2):
ch = random.choice(ch_all)
if ch not in ch_word:
ch_word.append(ch)
i=i+1
random.shuffle(ch_word)
print(ch_word)
answer = input('输入你猜的词:')
if(answer == word):
win()
else:
lose()
In [7]:
get_file()
ch_all = get_words('C:\\Users\\YSX-WDCZ\\Desktop\\new_idioms.txt')
word_choose('C:\\Users\\YSX-WDCZ\\Desktop\\new_idioms.txt',ch_all)
In [1]:
print(1)
In [ ]: