尽管输出正确但仍然出错

时间:2017-03-22 17:29:18

标签: python-3.x

尽管输出与预期输出完全相同,但仍会出现此错误消息。

*错误:预计会在可用字母行中找到一个数字:abcdefghijklmnopqrstuvwxyz。  检查以确保您的线与预期输出匹配! *

我的代码:

def hangman(secretWord):
    print ("Welcome to the game, Hangman!")
    print ("I am thinking of a word that is " + str(len(secretWord)) + " letters long" )

    lettersGuessed=[]
    guesses = 8
    p = " "
    while guesses > 0:

        print ("You have " + str(guesses) + " guesses left")

        print ("Available Letters: " + str(getAvailableLetters(lettersGuessed))),

        user_input = input("Please guess a letter: ")
        user_input = str(user_input)
        user_input = user_input.lower()

        if user_input  in lettersGuessed:
            print ("Oops! You've already guessed that letter: " + str(getGuessedWord(secretWord, lettersGuessed)))
        else:
            lettersGuessed.append(user_input)
            if user_input in secretWord:
                print ("Good guess: " + str(getGuessedWord(secretWord,lettersGuessed)))
                if isWordGuessed(secretWord, lettersGuessed):
                    break
                else: continue
            else:
                print("Oops! That letter is not in my word: " + str(getGuessedWord(secretWord, lettersGuessed)))
                guesses = guesses - 1
    p = str(getGuessedWord(secretWord, lettersGuessed))
    p = p.split(" ")
    p = "".join(p)
    p = str(p)

    if p == secretWord:
        print ("Congratulations, you won!")
        return
    else:
        print ("Sorry, you ran out of guesses. The word was " + secretWord + ".")
        return

output screenshot

1 个答案:

答案 0 :(得分:0)

检查每一行和“be sure your lines match up with the expected output”。 检查输出的自动化系统可能需要输出行必须完全匹配他们的“correct output包括 -----------行,以及你的代码不打印那些行。

轻松:

print ("I am thinking of a word that is " + str(len(secretWord)) + " letters long" )
print ("-----------")   ### <---- Add this one line
lettersGuessed=[]

print ("Good guess: " + str(getGuessedWord(secretWord,lettersGuessed)))
print ("-----------")   ### <---- Add this one line
if isWordGuessed(secretWord, lettersGuessed):

您可能需要在代码中的其他条件中添加一行,如else:条件。

为了使调试更容易,重复次数更少,代码更清晰,您可以将输出分配给变量并在需要时打印:

def hangman(secretWord):
    line_break = "-----------"
    ...
    print ("I am thinking of a word that is " + str(len(secretWord)) + " letters long" )
    print (line_break)

等。

希望这有帮助。