0

我正在尝试为我的 Hangman 游戏概念在同一行打印文本。它更早地工作了,虽然在从大多数错误中修复了游戏之后,我似乎无法让它工作。用于打印的代码是:

def printWord():
    guessedWords = []
    guessedWordsCorrect = []
    selectedWord = 'dog'
    printWordLength = 0
    printWordIndex = 0
    printWord = ''

    while printWordLength < len(selectedWord):
        if selectedWord[printWordIndex] == " ":
                print(" ",end='')
                printWordLength = printWordLength + 1
                printWordIndex = printWordIndex + 1
        else:
            if selectedWord[printWordIndex] in guessedWords:
                print(selectedWord[printWordIndex],"",end='')
                printWordLength = printWordLength + 1
                printWordIndex = printWordIndex + 1
            else:
                print("_ ",end='')
                printWordLength = printWordLength + 1
                printWordIndex = printWordIndex + 1
        print("")

我曾经end=""尝试在同一行打印,以前效果很好,但这次没有运气?

运行代码时,除了将它们打印在同一行之外,一切正常。

4

1 回答 1

1

在您打印没有换行符的内容之后,您正在每次循环迭代打印一个换行符:

while printWordLength < len(selectedWord):
    # ...
    print("")

将该打印语句移出循环:

while printWordLength < len(selectedWord):
    # ...

print("")
于 2013-10-15T17:20:31.540 回答