Zamdokie
Zamdokie

Reputation: 11

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:

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.

Upvotes: 1

Views: 624

Answers (1)

OTheDev
OTheDev

Reputation: 2967

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

word = random.choice(word_list)

Upvotes: 3

Related Questions