代码:
import random
word = ["python","jave","pig","eye","hello","word"]
print("欢迎来到猜单词小游戏")
choice = "y"
while choice == "y":
correct = random.choice(word) #随机选取元素
correct01 = correct #保存正确答案
mistake = ""
while correct:
a = random.randrange(len(correct))
#使用切片的方法
mistake = mistake + correct[a]
correct = correct[:a] + correct[a+1:]
print(mistake) #打印乱序后的单词
guess = input("请输入你的猜测:")
while guess != correct01 and guess != "n":
print("猜错了!请继续")
guess = input("猜测:")
if guess == correct01:
print("猜对啦!")
choice = input("是否继续(继续请输入y 退出请输入n):")
代码截图:
代码效果: