0

Could anyone give me some assistance with the following code? Everything is fine except one minor error. Basically, the code asks the user for a word. The word is taken and then masked. For example, let's say I enter the word: football

football is then converted to ** (one * for each letter). After that, the code will ask the user for a number of guesses to attempt. Let's say I enter 8 (exactly how long the word football is).

After 8 is entered, the user will be asked to give a guess 8 times, with each correct guess updating the masked string to show the guessed letter. The problem is that I want the program to automatically end right after the word has been revealed. For example, with football, each time a duplicate letter (ex. o and l) is entered, two letters are revealed and a guess attempt is skipped. So after football is entirely unmasked, the code/program still asks for 2 additional guesses. I don't want these two additional guesses, I want the program to end immediately. But I can't figure out how to do it.

I tried to set the following command at the end

if masked_secret_word == secret_word: break

But that doesn't seem to do anything. I thought it would match the fully guessed word with the original word and then end it but that doesn't seem to work. Anyone able to provide some assistance? Thank you.

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)
4

3 回答 3

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)

那么你提出的路线就可以了。

于 2013-07-12T10:01:01.897 回答
2

你的word_update()函数只打印,它不返回任何东西。该masked_secret_word函数内部的变量是局部变量,在函数外部不可见。

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
于 2013-07-12T09:58:19.270 回答
1

您永远不会更新masked_secret_word. 您可以将其退回,word_update()然后检查它是否与secret_word.

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

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
于 2013-07-12T10:04:58.237 回答