猜单词小游戏(python)

写一个python小程序,使其实现猜单词的小游戏

给定一些单词,从中随机抽取一个并打乱,提示用户输入正确顺序的单词,若输入正确提示用户输入正确并是否继续进行,若不正确则继续随机猜单词,直到猜正确

代码示例

#随即单词游戏
import random
WORDS = ("python", "jumnle","easy", "difficult","answer","continue","phone","position","game")
print("欢迎来到猜单词游戏!请把乱序后的单词拼接正确!!!")
isContinue = "y"
while isContinue =="y" or isContinue == "Y":
#选择单词
    words = random.choice(WORDS)
# print(words)
    right = words
    newword = ""
    while words:
#步长
        position = random.randrange(len(words))
#print(position)
# word = word + words[position]
        newword += words[position]
#print(newword)
        words = words[:position] + words[position+1:]  #前闭后开
    print("乱序后的单词:",newword)
    guess = input("请你猜正确的单词:")
# if guess = right:
#     print("恭喜你,猜对了!")
# else:
#     print("抱歉,你猜错了!")
    while guess != right and guess == "":
        print("抱歉,你猜错了")
        guess = input("请你继续猜的单词:")
    if guess == right:
        print("恭喜你,猜对了!")
        isContinue = input("选择是否继续游戏(y/n):")

运行示例

猜单词小游戏(python)_第1张图片

你可能感兴趣的:(python,python)