【发布时间】:2015-03-13 16:30:40
【问题描述】:
我想创建一个游戏,其中计算机选择一个随机单词,玩家必须猜测该单词。然后计算机会告诉玩家单词中有多少个字母。然后玩家有五次机会询问单词中是否有字母。计算机只能回答“是”或“否”。然后,玩家必须猜出这个词。
不知何故,我在下面编写的程序并没有准确地给玩家 5 次机会,让玩家在猜测单词之前询问字母是否在单词中。我可以知道出了什么问题吗?谢谢!
import random
WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)
correct = word
letters=len(word)
print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."
guess_letter = raw_input("Guess a letter in the word.")
tries = 0
while guess_letter in word:
tries +=1
print "Yes"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
while guess_letter not in word:
tries +=1
print "No"
guess_letter = raw_input("Guess another letter in the word.")
if tries == 4:
print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
print "That is correct!"
else:
print "You lose."
【问题讨论】:
-
请修正缩进
-
如果您只有 5 个更改,为什么不使用
for loop来代替?
标签: python loops variables while-loop