2.序列应用——猜单词游戏

# WORD Jumble猜单词游戏
import random
# 建立单词序列
WORDS = ("aaa","sss","abc")
# 开始游戏
print(
    """
        欢迎参加猜单词游戏
    把字母组合成一个正确的单词。
    """
)
iscontinue = "y"
while iscontinue == "y" or iscontinue == "Y":
    # 从序列中随机挑出一个单词
    word = random.choice(WORDS)
    # 一个用于判断玩家是否猜对的变量
    correct = word
    # 建立乱序后单词
    jumble = ""dom

    while word:                  # word不是空串循环
        # 根据word长度产生word的随机位置
        position = random.randrange(len(word))
        # 将position位置的字母组合到乱序后单词
        jumble += word[position]
        # 经过切片将position位置的字母从原单词中删除
        word = word[: position] + word[(position + 1):]
    print("乱序后单词:", jumble)blog

    guess = input("\n 请你猜:")
    while guess != correct and guess != "":
        print("对不起不正确。")
        guess = input("继续猜:")游戏

    if guess == correct:
        print("真棒,你猜对了!\n")
    iscontinue = input("\n 是否继续(Y/N)")input

============================================================================it

运行结果截图:io

相关文章
相关标签/搜索