Python Hangman“for”循环将替换所有字符,而不仅仅是正确的字符

2024-05-23 18:01:26 发布

您现在位置:Python中文网/ 问答频道 /正文

在我理解下面的函数时,如果用户键入“Z”,它应该循环遍历“斑马”中的每个字符;<强>如果字符匹配用户的输入,则应该用用户的条目替换用户词(空白单词)中相应的字符。否则,它应该什么也不做,继续循环使用“斑马”这个词。你知道吗

因此,如果用户猜到一个“z”,它应该识别出“z”是变量word中的字符0,然后用“z”替换user\u word中的字符0(“\uuuuuuuuuuuuuuuuu”)。你知道吗

相反,如果用户猜到任何正确的字母,它会用用户的输入替换“\uuuuuuuuuuuuuuu”中的每个字符。所以,如果user\u guess=“z”,user\u word打印为“zzzzz”。如果他/她猜到“e”,用户的单词会打印为“eeeee”,以此类推。你知道吗

我遵循了程序的逻辑,一步一步,从纸面上看,它似乎应该是可行的。下面我做错了什么?你知道吗

我知道下面有太多的“if/else”语句,我确实打算将其中的一些语句压缩到单独的函数中(比如“correctguess()”函数)。你知道吗

word = "zebra"

def hangman(word):

    user_word = "_" * len(word)
    incorrect_guesses = []
    failed_guesses = 0
    user_guess = getuser()

    while user_word != word and failed_guesses < 10:
        if user_guess in word:
            for char in word:
                if char == user_guess:
                    user_word = user_word.replace(user_word[word.index(char)], user_guess)
        else:
            incorrect_guesses.append(user_guess)
            failed_guesses += 1

        print "Word: ", user_word
        print "Letters missed: ", ', '.join(incorrect_guesses)
        user_guess = getuser()

    if user_guess == word:
        print "Awesome, you got it! You win!"

    else:
        print "That's ten tries, your dude is hanging from the gallows. You lose :("

Tags: 函数用户if字符单词elsewordprint
2条回答

.replace()并不像您认为的那样

.replace用第二个替换第一个paramater的所有出现。举个例子:

"sentence".replace("e", "z")

将给出:

"szntzncz"

在程序中要做的是在循环遍历每个char时,用char替换的索引,即并非所有字符都加下划线_。你知道吗

要替换字符串的索引,需要将其转换为带有list()的列表,然后更改所需的index,最后转换回带有''.join()的字符串。你知道吗

所以这条线:

user_word = user_word.replace(user_word[word.index(char)], user_guess)

应改为:

user_word = list(user_word)
user_word[word.index(char)] = user_guess
user_word = ''.join(user_word)

这会给你5个下划线中z的正确位置。你知道吗

可能还需要考虑将单词作为chars存储在一个列表中,因为这意味着您不必在每次猜测字母时都进行转换。如果您将它存储在一个列表中,您只需记住在为用户显示它时打印:''.join(user_word)。:)

问题出在这里:

for char in word:
    if char == user_guess:
        user_word = user_word.replace(user_word[word.index(char)], user_guess)

如果字符char确实是user_guess的字符,那么user_word[word.index(char)]将是下划线字符'_',因此您调用:

user_word = user_word.replace('_',user_guess)

这意味着您将用user_guess替换所有下划线,而不管这些下划线是否位于user_guess位于word的位置。你知道吗

可以使用以下表达式代替for循环:

user_word = ''.join(w if w == user_guess else u for u,w in zip(user_word,word))

其工作原理如下:字符串是iterable。如果在字符串周围写一个循环,则对字符进行迭代。通过使用zip,我们构造了两个iterables的元组。因此,这里的初始运行将产生('_', 'z'), ('_', 'e'), ('_', 'b'), ('_', 'r'), ('_', 'a')。对于每个元组,我们在u,w(因此第一次迭代u = '_'w='z')中解压它们。你知道吗

然后我们使用三元运算符w if w == user_guess else u。因此,三元运算符将检查word的字符w是否与user_guess相同。在这种情况下,我们知道用w替换字符,否则我们得到u。你知道吗

我们用这个表达式生成的iterable因此生成字符,我们将这些字符放在一起。你知道吗

所以正确的程序是:

word = "zebra"

def hangman(word):

    user_word = "_" * len(word)
    incorrect_guesses = []
    failed_guesses = 0
    user_guess = getuser()

    while user_word != word and failed_guesses < 10:
        if user_guess in word:
            user_word = ''.join(w if w == user_guess else u for u,w in zip(user_word,word))
        else:
            incorrect_guesses.append(user_guess)
            failed_guesses += 1

        print "Word: ", user_word
        print "Letters missed: ", ', '.join(incorrect_guesses)
        user_guess = getuser()

    if user_guess == word:
        print "Awesome, you got it! You win!"

    else:
        print "That's ten tries, your dude is hanging from the gallows. You lose :("

如果我设置getuser = input,我将运行以下命令:

>>> hangman('zebra')
a
Word:  ____a
Letters missed:  
b
Word:  __b_a
Letters missed:  
c
Word:  __b_a
Letters missed:  c
d
Word:  __b_a
Letters missed:  c, d
e
Word:  _eb_a
Letters missed:  c, d
f
Word:  _eb_a
Letters missed:  c, d, f
g
Word:  _eb_a
Letters missed:  c, d, f, g
h
Word:  _eb_a
Letters missed:  c, d, f, g, h
i
Word:  _eb_a
Letters missed:  c, d, f, g, h, i
j
Word:  _eb_a
Letters missed:  c, d, f, g, h, i, j
k
Word:  _eb_a
Letters missed:  c, d, f, g, h, i, j, k
l
Word:  _eb_a
Letters missed:  c, d, f, g, h, i, j, k, l
m
Word:  _eb_a
Letters missed:  c, d, f, g, h, i, j, k, l, m
n
That's ten tries, your dude is hanging from the gallows. You lose :(

相关问题 更多 >