python 2.7 - In a While loop using random.choice, how to always pick a random letter? -
here's code:
# guess word program # roxmate # 12/04/2013 import random print \ """\t\t\t ***welcome guess word program*** goal guess word computer has chosen , have 5 hints per guess, type first one. if wish exit 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 computer choosed has", gen_word_1, "letters\n" guess = raw_input("your guess:") while guess != quit_game: if guess == gen_word: print "congrats, have guessed word!" break elif guess == hint: print "hint:: there a(n)", hint_letter, "in word\n" else: if guess != gen_word: print "i sorry that's not word, try again.\n" guess = raw_input("your guess:") raw_input()
the problem @
elif guess == hint: print "hint:: there a(n)", hint_letter, "in word\n"
i don't understand why gives same letter hint, shouldn't pick random letter word, each time loop runs? example type in , gives me a, next time typed in give me b.
by doing
hint_letter = random.choice(gen_word)
you call function once , store return value. you're printig value on , on again. should do:
print "hint:: there a(n)", random.choice(gen_word), "in word\n"
instead.
Comments
Post a Comment