思路:首先创建一个单词组,再从中随机抽取一个单词,再把这个单词打乱后重组,最后判断所猜单词是否正确。
有些为上下文贯通,如有不懂,请先往下看。
1.定义单词组
2.从单词组中抽取随机的单词
3.把所抽取的单词打乱并重组
4.判断是否正确
写完后Ctrl+S保存,按F5运行测试
如有需要代码自取
#定义一个单词组words
words=('python','jumble','easy','difficult','answer'
,'continue','phnoe','position','pose','game')
print(
"""
欢迎参加猜单词游戏
把字母组合成一个正确的单词
"""
)
#定义一个是否继续的值(输入y/Y继续)
iscontinue='y'
while iscontinue=='y' or iscontinue=='Y':
#random.choice()为从words里随机抽取一个元素
import random
word=random.choice(words)
#correct为判断玩家是否猜对的变量
correct=word
#定义b为打乱顺序后的单词
b=''
while word:
#len()是用来获取上面随机产生word的长度
#random.randrange()可以生成指定范围的随机数
#根据产生word的长度,锁定同样长度随机的单词
a=random.randrange(len(word))
#将a字母组合到乱序后单词b
b+=word[a]
#通过切片,将a产生的字母删除
word=word[:a]+word[(a+1):]
print('乱序后单词',b)
guess=input('\n请你猜:')
while guess!=correct and guess!='':
print('对不起不正确.')
guess=input('继续猜:')
if guess==correct:
print('真棒,你猜对了!\n')
iscontinue=input('\n\n是否继续(Y/N):')