设计一个猜单词游戏,打乱字母顺序,供玩家猜测。

随机模块为:random

 

import random

WORDS = ('python', 'hello', 'time', 'apple', 'game', 'phone', 'difficult', 'easy', 'juice')  # 存储一些单词

word = random.choice(WORDS)  # 随机选择一个单词
wordsave = word  # 选择出的单词暂存
tmp = ''  # 打乱顺序的词的存储位置
while word:
    position = random.randrange(len(word))  # word中随机选一次字母
    tmp += word[position]  # 选出的字母添加到tmp中
    word = word[:position] + word[(position + 1):]  # 不断的随机选出字母,word不断减小直至为空
print('乱序后的单词为:', tmp)
a = input('请输入猜测的原单词:')
if a == wordsave:  # 和暂存单词比较
    print('正确')
else:
    print('错误')