0

这是代码:

# 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() 

问题是在

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

我不明白为什么它只给出相同的字母作为提示,它不应该在每次循环运行时从单词中随机选择一个字母吗?例如,我输入帮助,它给了我a,下次我输入帮助它会给我b。

4

2 回答 2

2

通过做

hint_letter = random.choice(gen_word)

您调用该函数一次并存储返回值。然后你一遍又一遍地打印那个值。你应该做:

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

反而。

于 2013-05-13T23:00:57.960 回答
0

您只执行一次随机选择。如果您希望它继续发生,则需要在循环中进行。循环不会神奇地在它之外运行所有代码。

于 2013-05-13T22:57:16.347 回答