简体   繁体   English

Python:猜词游戏 - WHILE 循环出现问题

[英]Python: Word Guessing Game - trouble with WHILE Loop

import random
import time

word_list = ['snack','snore','place']

guess = ''
word = ''

while True:
    guess = input("Guess a word: ")
    word = random.choice(word_list)

    if guess == word: 
        time.sleep(.9)
        print (f"Congrats, you won! the word was {word}")
        time.sleep(.5)
        print() 

        play = (input("If you want to play again, hit ENTER otherwise type n: ")) 
        if play != '':
            break
        else:
            print("Let's play again")

I am working on a 'word' guessing game.我正在做一个“单词”猜谜游戏。 User has to guess one of the three words to win.用户必须猜出三个词之一才能获胜。 I want the loop to continue for as long as the user wants to play.只要用户想玩,我就希望循环继续下去。 But it seems that once they play once, the random word choice isn't working.但似乎一旦他们玩了一次,随机选词就不起作用了。 Here is an example of my output:这是我的 output 的示例:

Guess a word: snack
Guess a word: snore
Guess a word: place
Guess a word: snack
Guess a word: place
Congrats, you won! the word was place

You can see that the word was place, but I had already guessed that word, it wasn't until I guessed it again that it worked.你可以看到这个词是地方,但是我已经猜到了那个词,直到我再次猜到它才起作用。

Any help on this is greatly appreciated.非常感谢对此的任何帮助。 I am brand new at Python and just trying to figure out the foundation of this software to the best of my ability.我是 Python 的新人,我只是想尽我所能弄清楚这个软件的基础。

Your word keeps changing.你的话一直在变。 Move移动

word = random.choice(word_list)

outside the loop (just above).循环外(就在上面)。

If the user wants to continue playing, you will need to draw another random word.如果用户想继续玩,就需要再随机抽一个字。 So in the else clause, also put所以在else子句中,也放

word = random.choice(word_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM