0
print("Welcome to Hangman! Guess the mystery word with less than 6 mistakes!")

words= ['utopian','fairy','tree','monday','blue'] 

i=int(input("Please enter a number (0<=number<10) to choose the word in the list: "))

if(words[i]):
    print("The length of the word is: " , len(words[i]))


guess=input("Please enter the letter you guess: ")


if(guess in words[i]):
    print("The letter is in the word.")

else:
    print("The letter is not in the word.")

guesses=1

while guesses<6:
    guess=input("Please enter the letter you guess: ")


if(guess in words[i]):
    print("The letter is in the word.")
    guesses=guesses+1

else:
    print("The letter is not in the word.")
    guesses=guesses+1

if guesses==6:

    print("Failure. The word was:" , words[i])

刚刚开始在 python 中执行这个 Hangman 程序。我正在根据一组说明逐步执行此操作,并且我现在想编写一些简单的代码来检查是否在所选单词中找到了输入的字母。我忽略了比赛的位置,只关心计算错误猜测的数量。到目前为止看起来还不错,但是我遇到了一个小而大(如果有意义的话)的问题。在第六猜想我希望循环完成并让用户知道他们失败并且计算机赢了。我注意到,在我的例子中,一旦用户输入了他们的第六次猜测,无论是坏的还是好的,循环就结束了。因此,如果这个词是“仙女”或其他什么,无论用户猜到多少正确字母,循环都会在他们的第 6 次循环中完成。我希望循环仅在用户输入六个错误猜测时完成,因此在“fairy”这个词的示例中,如果用户输入“f”这将是正确的,如果接下来的六个猜测不正确,则“失败”消息将打印出来,而不是我现在所拥有的。

4

2 回答 2

1

你有这个代码:

if(guess in words[i]):
    print("The letter is in the word.")
    guesses=guesses+1

如果您删除最后一行,那么好的猜测将不会对您不利。

另外,我会小心你的空白。你现在有问题的方式,只有guess=input("Please enter the letter you guess: ")一行在while循环中。不过,我猜想将代码放在 StackOverflow 上是一个简单的错误。

于 2013-10-21T22:04:54.880 回答
1

这是一个有点不合常规的答案,但这里有一些带有评论的编辑代码。

print("Welcome to Hangman! Guess the mystery word with less than 6 mistakes!")

words= ['utopian','fairy','tree','monday','blue'] 

i=int(raw_input("Please enter any number to choose the word in the list: "))%5
#the %6 here means divide the input by six and take the remainder as the answer. I found the instructions a little confusing, and this allows the user to be competely unrestricted in the number that they choose while still giving you a number (0, 1, 2, 3, or 4) that you want. Increasing the number after the % will give you a larger variety of choices.

# if(words[i]):
#this line is unnecessary with the above code, and also would not prevent errors futher along in the code if i was not a number between 0 and 4.

print "Your chosen word is", len(words[i]), "characters long."
# slight wording change here
#guess=input("Please enter the letter you guess: ")
#if(guess in words[i]):
#print("The letter is in the word.")
#else:
#print("The letter is not in the word.")
#guesses=1
# all of this is already accomplished in the loop you wrote below.

incorrect_guesses = 0
# it's always nice to initialize a variable
while incorrect_guesses<6:
    guess=str(raw_input("Please enter a letter to guess: "))
    # Unless you're using python 3, I would use raw_input rather than input.


    if((guess) in words[i]):
        print('Correct! The letter "' + guess + '" appears in the word ' + str(words[i].count(str(guess))) + ' times.')
    else:
        print('Sorry, the letter "' + guess +
              '" is not in the word.')
        incorrect_guesses=incorrect_guesses+1
# Plusses instead of commas clean up the output a bit, and str(words[i].count(str(guess))) gives you the string form of the number which indicates how many times the guess appears in the word. This is useful in words like "tree".
print("Failure. The word was:" , words[i])
# This is outside the while loop because it gets triggered as soon as incorrect guesses go over 6.
# You can still improve the program by adding a feature which tells the players that they have guessed all of the correct letter and telling them what the word was, and possibly by increasing the word list. You can also tell players when they have already guessed a letter in case they've forgotten.

希望这对您未来的 Python 工作有所帮助。

于 2013-10-21T23:01:41.627 回答