user2371866
user2371866

Reputation: 1

In a While loop using random.choice, how to always pick a random letter?

here's the code:

# Guess the word program
# By Roxmate
# 12/04/2013

import random

print \
    """\t\t\t ***Welcome to Guess the Word Program***

        Your goal is to guess what word the computer has chosen and
        you have 5 hints per guess, just type help to get the first one.
        If you wish to exit the program, type in exit.

        \t\t\tGood Luck!
    """

WORDS = ("book","house","universe")

gen_word = random.choice(WORDS)
gen_word_1 = len(gen_word)
hint_letter = random.choice(gen_word)
hint = "help"
quit_game = "exit"

print "The word that the computer choosed has", gen_word_1, "letters\n"

guess = raw_input("Your Guess:")
while guess != quit_game:
    if guess == gen_word:
        print "Congrats, you have guessed the word!"
        break
    elif guess == hint:
        print "Hint:: There is a(n)", hint_letter, "in the word\n"

    else:
        if guess != gen_word:
            print "I am sorry that's not the word, try again.\n"



    guess = raw_input("Your Guess:")


raw_input() 

The problem is that at

  elif guess == hint:
        print "Hint:: There is a(n)", hint_letter, "in the word\n"

i don't understand why it only gives the same letter as a hint, shouldn't it pick a random letter from the word, each time the loop runs? For example i type in help and it gives me a, next time i typed in help it would give me b.

Upvotes: 0

Views: 2247

Answers (2)

Robsdedude
Robsdedude

Reputation: 1403

By doing

hint_letter = random.choice(gen_word)

you call the function once and store the return value. Then you're printig that value over and over again. You should do:

print "Hint:: There is a(n)", random.choice(gen_word), "in the word\n"

instead.

Upvotes: 2

Marcin
Marcin

Reputation: 49826

You only perform the random choice once. If you want it to keep happening, you need to do it in the loop. The loop doesn't magically run all of your code outside of it.

Upvotes: 0

Related Questions