Python小程序之猜单词游戏

计算机随机产生一个单词,打乱字母顺序,供玩家去猜。(可供猜的单词自己定义)

import random
WORDS = ("math","english","china","history")
right = 'Y'
print("欢迎参加猜单词游戏!")

while right=='Y' or right=='y':
    word=random.choice(WORDS)
    correct=word
    newword = ''
    while word:
        pos=random.randrange(len(word))
        newword+=word[pos]
        #将word单词下标为pos的字母去掉,取pos前面和后面的字母组成新的word
        word = word[:pos]+word[(pos+1):] #保证随机字母出现不会重复
    print("你要猜测的单词为:",newword)
    guess = input("请输入你的答案:")
    count=1
    while count<5:
        if guess!=correct:
            guess = input("输入的单词错误,请重新输入:")
            count+=1
        else :
            print("输入的单词正确,正确单词为:",correct)
            break
    if count == 5:
        print("您已猜错5次,正确的单词为:",correct)

    right = input("是否继续,Y/N:")
    
        

猜你喜欢

转载自blog.csdn.net/fbvukn/article/details/85782909