import random # 创建单词序列,并去除重复项 WORDS = list(set(("python", "jumble", "easy", "difficult", "answer", "continue", "phone", "position", "game"))) print( """ 欢迎参加猜单词游戏 把字母组合成一个正确的单词. """ ) #是否继续 is_continue = "y" while is_continue.lower() == "y": # 从序列 WORDS 中随机挑出一个单词 word = random.choice(WORDS) # 创建乱序后单词 jumble混乱的东西 jumble = "" # 将单词转为列表,方便删除字符 temp临时的 temp_word = list(word) #while 循环判断 while temp_word: position = random.randrange(len(temp_word)) # pop删除并返回指定位置的元素 jumble += temp_word.pop(position) print("乱序后单词:", jumble) #guess 猜测,推测 guess = "" while guess != word: guess = input("\n请你猜: ").lower() # 转换为小写以忽略大小写 if guess == "": print("你放弃了猜测。") break elif guess != word: print("对不起不正确.") if guess == word: print("真棒,你猜对了!\n") # 游戏结束后,重新询问是否继续 is_continue = input("\n\n是否继续(Y/N):") print("游戏结束,谢谢参与!")
猜单词(简单版)
最新推荐文章于 2024-07-12 16:16:27 发布