案例代码为:
#导入库random
import random
#建立英文单词库,以元组的形式表示
WORDS = ['apple','pear','banana','cherry','good','better','best',
'python','while','tuple','dictionary','jumble','difficult',
'aesthetic','stereotype','civilization','anniversary']
print("欢迎参加猜单词游戏!\n请把乱序后的字母组成一个单词\n")
isContinue = "Y" #刚进入游戏时默认选择为Y
while isContinue in ("Y", "y"):
#随机挑选一个单词
word = random.choice(WORDS) #在WORDS中随机选择一个单词
answer = word #保存答案
#将选出的单词进行乱序
jumble = ""
for i in word:
#随机抽取一个位置的字符放入乱序jumble中,并从原word中删除该字符
position = random.randrange(len(word)) #position 为不大于被选中单词字母的个数的随机数
jumble += word[position] #放入jumble中
word = word[:position] + word[(position+1):] #看下方注释
#输出乱序后的单词
print(&