我在函数中修改主变量时没有更新

时间:2013-07-12 09:55:14

标签: python

有人可以通过以下代码给我一些帮助吗?一切都很好,除了一个小错误。基本上,代码会询问用户一个单词。这个词被采取然后被掩盖。例如,假设我输入单词:football

然后将足球转换为 * * (每个字母一个*)。之后,代码将要求用户进行多次猜测。假设我输入8(足球这个词到底有多长)。

输入8后,将要求用户进行8次猜测,每次正确猜测都会更新屏蔽的字符串以显示猜测的字母。问题是我希望程序在单词显示后自动结束。例如,对于足球,每次输入重复的字母(例如o和l)时,会显示两个字母并跳过猜测尝试。因此,在足球完全被揭露之后,代码/程序仍然需要另外两个猜测。我不想要这两个额外的猜测,我希望程序立即结束。但我无法弄明白该怎么做。

我尝试在最后设置以下命令

如果masked_secret_word == secret_word:     破

但这似乎没有做任何事情。我认为它会将完全猜到的单词与原始单词相匹配,然后结束它,但这似乎不起作用。有人能提供一些帮助吗?谢谢。

def word_update (secret_word, guessed_letters):
    masked_secret_word = ""
    for letter_guess in secret_word:
        if letter_guess in guessed_letters:
            masked_secret_word += letter_guess
        else:
            masked_secret_word += "*"
    print "Secret Word update:" + masked_secret_word

alphabet = map(chr, range(97, 123))

secret_word = raw_input ("Type in the secret word: ").lower()
while secret_word.isalpha() == False:
    secret_word = raw_input ("ERROR: Type in the secret word").lower()
masked_secret_word = len(secret_word) * "*"
guesses = raw_input ("How many guesses will you allow?")
while guesses.isdigit() == False or 0 >= int(guesses):
    guesses = raw_input ("ERROR: How many guesses will you allow?")
guesses = int(guesses)
while len(secret_word) > guesses:
    guesses = raw_input ("ERROR: The number of guesses is smaller than the word")
    while guesses.isdigit() == False or 0 >= int(guesses):
        guesses = raw_input ("ERROR: How many guesses will you allow?")
    guesses = int(guesses)
print "Secret Word = " + masked_secret_word
guessed_letters = []
while guesses != 0:
    letter_guess = raw_input ("Guess a letter (a-z)? ")
    while letter_guess not in alphabet:
        letter_guess = raw_input ("ERROR: Guess a letter (a-z)? ")
    if letter_guess in guessed_letters:
        print "That letter has already been guessed."
    else:
        guesses = guesses - 1
        guessed_letters.append(letter_guess)
        word_update(secret_word, guessed_letters)

3 个答案:

答案 0 :(得分:3)

word_update()函数中,创建一个名为masked_secret_word的局部变量。这与主代码中的masked_secret_word不同,而是一个新的局部变量。

您实际上并未更新主masked_secret_word变量,它仍然是一系列'*'

最小的解决方法是添加

global masked_secret_word

word_update()函数中,但更好的方法是让你的word_update()函数返回新的masked_secret_word并使用它来更新全局变量:

masked_secret_word = word_update(secret_word, guessed_letters)

然后你提议的线路会起作用。

答案 1 :(得分:2)

您的word_update()功能仅打印,不会返回任何内容。该函数中的masked_secret_word变量是 local 变量,在函数外部不可见。

word_update()函数之外,masked_secret_word是一个不同的变量,其值为'*******',因此您的secret_word值永远不会与之匹配。

从函数返回一个值以传回给调用者:

def word_update (secret_word, guessed_letters):
    masked_secret_word = ""
    for letter_guess in secret_word:
        if letter_guess in guessed_letters:
            masked_secret_word += letter_guess
        else:
            masked_secret_word += "*"
    print "Secret Word update:" + masked_secret_word
    return masked_secret_word == secret_word  # returns True or False

然后测试该返回值:

if word_update(secret_word, guessed_letters):
    break

答案 2 :(得分:1)

您永远不会更新masked_secret_word。您可以在word_update()中将其返回,然后检查它是否与secret_word匹配。

这是完整的代码 - 我在word_update()中添加了一个return语句,并检查了主循环中的相等性:

def word_update (secret_word, guessed_letters):
    masked_secret_word = ""
    for letter_guess in secret_word:
        if letter_guess in guessed_letters:
            masked_secret_word += letter_guess
        else:
            masked_secret_word += "*"
    print "Secret Word update:" + masked_secret_word
    return masked_secret_word

alphabet = map(chr, range(97, 123))

secret_word = raw_input ("Type in the secret word: ").lower()
while secret_word.isalpha() == False:
    secret_word = raw_input ("ERROR: Type in the secret word").lower()
masked_secret_word = len(secret_word) * "*"
guesses = raw_input ("How many guesses will you allow?")
while guesses.isdigit() == False or 0 >= int(guesses):
    guesses = raw_input ("ERROR: How many guesses will you allow?")
guesses = int(guesses)
while len(secret_word) > guesses:
    guesses = raw_input ("ERROR: The number of guesses is smaller than the word")
    while guesses.isdigit() == False or 0 >= int(guesses):
        guesses = raw_input ("ERROR: How many guesses will you allow?")
    guesses = int(guesses)
print "Secret Word = " + masked_secret_word
guessed_letters = []
while guesses != 0:
    letter_guess = raw_input ("Guess a letter (a-z)? ")
    while letter_guess not in alphabet:
        letter_guess = raw_input ("ERROR: Guess a letter (a-z)? ")
    if letter_guess in guessed_letters:
        print "That letter has already been guessed."
    else:
        guesses = guesses - 1
        guessed_letters.append(letter_guess)
        masked_secret_word = word_update(secret_word, guessed_letters)
        if masked_secret_word == secret_word:
            # You win
            break