Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Hangman程序:循环中不正确使用全局变量_Python 3.x_While Loop_Global Variables - Fatal编程技术网

Python 3.x Hangman程序:循环中不正确使用全局变量

Python 3.x Hangman程序:循环中不正确使用全局变量,python-3.x,while-loop,global-variables,Python 3.x,While Loop,Global Variables,我正在编写一个程序来玩游戏hangman,但我认为我没有正确使用全局变量 一旦程序的第一次迭代在正确猜测后结束,任何具有正确猜测的后续迭代都会打印单词及其所有过去的值 如何仅打印word的最新值?这段代码位于while循环中,在该循环中,每次迭代都会获得用户输入。谢谢 代码: 输出: #first iteration if 'a' was guessed: a _ _ _ _ #second iteration if 'l' was guessed: a _ _ _ _ a _ _ l _

我正在编写一个程序来玩游戏hangman,但我认为我没有正确使用全局变量

一旦程序的第一次迭代在正确猜测后结束,任何具有正确猜测的后续迭代都会打印
单词及其所有过去的值

如何仅打印
word
的最新值?这段代码位于while循环中,在该循环中,每次迭代都会获得用户输入。谢谢

代码:

输出:

#first iteration if 'a' was guessed:
a _ _ _ _

#second iteration if 'l' was guessed:
a _ _ _ _ a _ _ l _

#third iteration if 'e' was guessed:
a _ _ _ _ a _ _ l _ a _ _ l e

#Assuming the above, for the third iteration I want:
a _ _ l e

注意:这只是我的代码的一小部分,但我觉得其他的代码块并不相关。

每次调用函数
getGuessedWord
时,您都要添加到“word”,不能使用全局:

secretWord = "myword"


def getGuessedWord(secretWord, lettersGuessed):
    word = ""
    for letter in secretWord:
        if letter not in lettersGuessed:
            word=word+' _'
        elif letter in lettersGuessed:
            word=word+' '+letter
    return print(word)


getGuessedWord(secretWord,"")
getGuessedWord(secretWord,"m")
getGuessedWord(secretWord,"mwd")
或者你可以通过将单词设置为一个恒定的长度来解决这个问题(没有那么好和难理解),例如:
word='''.*len(secretWord)
,然后替换字母
word=word[:2*i]+letter+word[2*i+1:][/code>

示例如下:

secretWord = "myword"
word='_ '*len(secretWord)

def getGuessedWord(secretWord, lettersGuessed):
    global word
    for i, letter in enumerate(secretWord):
        if letter in lettersGuessed:
            word=word[:2*i]+letter +word[2*i+1:]
    return print(word)

getGuessedWord(secretWord,"")
getGuessedWord(secretWord,"m")
getGuessedWord(secretWord,"w")
getGuessedWord(secretWord,"d")

您面临的主要问题是,每次调用函数时都要追加全局变量。但是,我认为您不需要使用全局变量,通常这是一种非常糟糕的做法,考虑到您在问题中的解释,您可以简单地使用以下代码:

def getGuessedWord(secretWord, lettersGuessed):
    return ' '.join(letter if letter in lettersGuessed else '_'
                    for letter in secretWord)

我还认为,如果您使用python理解来加快代码速度会更好。

正如@lmiguelvargasf在其更清晰的答案中提到的,使用列表理解更好。我已经为您的问题添加了一个答案。请让我知道这是否有用。
def getGuessedWord(secretWord, lettersGuessed):
    return ' '.join(letter if letter in lettersGuessed else '_'
                    for letter in secretWord)