python基础“猜单词游戏”代码

"""
程序功能:Werd Jumble猜单词游戏
编写人员:孙三岁
编写日期:2022/9/10
"""
import random

# 创建单词序列
WORDS = (
    "python", "jumble", "easy", "difficult", "answer",
    "continue", "phon", "position", "result", "game"
)
# start the game
print(
    """
       欢迎参加猜单词游戏
    把字母组合成一个正确的单词
    """
)
iscontinue = "y"
while iscontinue == "y" or iscontinue == "Y":
    # 从程序中随机挑选出一个单词
    word = random.choice(WORDS)
    # 一个用于判断玩家是否猜对的变量
    correct = word
    # 创建乱序后单词
    jumble = ""
    while word:  # word不是空串时循环
        # 根据word长度,产生word的随机位置
        position = random.randrange(len(word))
        # position位置字母从原单词中删除
        jumble += word[position]
        # 通过切片,将position位置字母从原单词中删除
        word = word[:position] + word[(position + 1):]
        print("\n\n乱序后单词:", jumble)
        guess = input("请你猜:")
        while guess != correct and guess != "":
            print("对不起不正确")
            guess = input("请继续猜:")
        if guess == correct:
            print("真棒,你猜对了!")
            iscontinue = input("是否继续(Y/N):")

猜你喜欢

转载自blog.csdn.net/Sjm05/article/details/126792802