这是代码:
# 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"
我不明白为什么它只提供相同的字母作为提示,每次循环运行时,它不应该从单词中选择一个随机字母吗?例如,我输入帮助,它给了我一个,下次我输入帮助它会给我b。
答案 0 :(得分:2)
通过
hint_letter = random.choice(gen_word)
您调用该函数一次并存储返回值。 然后你一遍又一遍地重视这个价值。 你应该这样做:
print "Hint:: There is a(n)", random.choice(gen_word), "in the word\n"
代替。
答案 1 :(得分:0)
您只执行一次随机选择。如果你想让它继续发生,你需要在循环中完成它。循环不会神奇地运行你之外的所有代码。