【问题标题】:10.1.3 Guess the Word, Part 2 Help Needed10.1.3 猜词,第 2 部分 需要帮助
【发布时间】:2021-01-13 23:20:22
【问题描述】:

我目前在 CodeHS 上进行练习 10.1.3,它本质上是制作刽子手游戏的一部分,没有刽子手。对于游戏的这一部分,我的代码几乎完成了,但由于某种原因,每当给出新的输入(对于一个字母),它都会清除以前的输入。

这是我的代码:

secret_word = "character"
dashes = "-" * len(secret_word)
print(dashes)
#Prints the first row of dashes to indicate word length

def get_guess():
    while True:
        guess = input("Guess: ")
        if isinstance(guess, int):
            print("Your guess must be a lowercase letter.")
        else:
            if guess.islower():
                if len(guess) == 1:
                    return guess
                else:
                    print("Your guess must have exactly one character.")

def update_dashes(secret_word, dashes, guess):
    result = ""
    for i in range(len(secret_word)):
        if secret_word[i] == guess:
            result = result[:i] + guess + result[i + 1:]
        else:
            result = result + "-"
    return result

while True:
    guess = get_guess()
    if guess in secret_word:
        print("The letter is in the word.")
    else:
        print("That letter is not in the word.")
    print(update_dashes(secret_word, dashes, guess))
#We believe the issue is here, it is printing the full set each time this loops.

运行时,输出如下:

---------
Guess: c
The letter is in the word.
c----c---
Guess: h
The letter is in the word.
-h-------
Guess: 

代码应该在单词中添加“h”,但它正在“覆盖”已经存在的内容。

这是我想要打印的内容:

---------
Guess: c
The letter is in the word.
c----c---
Guess: h
The letter is in the word.
ch---c---
Guess: 

注意,应该猜的词是“字符”。

最后,这是与当前问题相关的作业部分:

第 2 步 - 更新破折号

这是棘手的部分。编写一个名为 update_dashes 的函数 接受三个字符串参数 - 秘密词,当前状态 破折号,和最近的猜测 - 并返回新的状态 破折号。

因此,例如,如果单词是“茄子”,则当前值 破折号是“e-------”,猜测是“g”,函数应该返回 “蛋-----”。

如果单词是“茄子”,破折号的当前值为“e-------”, 并且猜测是“z”,函数应该返回“e-------”(什么都没有 变化,因为猜测不正确)。

你可以这样做。

在您的函数中,从一个空的结果字符串开始。写一个for循环 从 0 到但不包括您的密码的长度。说你 在你的 for 循环中使用变量 i 。比较中的字符 索引 i 处的 secret_word 猜测。如果它们相等,那么这意味着 猜测匹配单词中的那个字母,所以你应该加上猜测 到结果。否则,猜测与该字母不匹配 单词,所以你应该将索引 i 处的任何内容以破折号添加到你的 结果。

等等,我为什么不在第二种情况下添加一个破折号?

如果您只是在猜测与字符不匹配时添加一个破折号 在索引 i 处的 secret_word 中,这将消除任何正确的猜测 用户已经做出来了!想象一下这个词是“茄子”的情况, 破折号的状态是“e——-”,猜测是“g”。如果你总是添加 当猜测与 secret_word 中的字符不匹配时出现破折号 索引 i,结果将是“-gg——”。突然,“e”不见了, 因为“g”与“e”不匹配!通过在索引 i 处使用破折号,您 可能会附加一个字母或破折号,具体取决于是否 用户在当前猜测之前已经猜到了那个字母。

一旦你的 for 循环完成,你的结果字符串应该有字母和 破折号在所有正确的地方,所以你可以把它归还!

【问题讨论】:

  • input() 总是返回一个字符串,所以isinstance(guess, int) 永远不会是真的。
  • 您所做的正是它在“为什么我不在第二种情况下添加破折号?”中所说的不应该做的事情

标签: python


【解决方案1】:

正如说明所说,当猜测不匹配时,您应该将dashes 的相应元素添加到结果中,而不仅仅是破折号。您需要这样做,以便将之前的所有正确猜测复制到结果中。

def update_dashes(secret_word, dashes, guess):
    result = ""
    for i in range(len(secret_word)):
        if secret_word[i] == guess:
            result += guess
        else:
            result += dashes[i]
    return result

此外,您的主循环必须将结果分配回dashes,以便累积所有正确的猜测。

while True:
    guess = get_guess()
    if guess in secret_word:
        print("The letter is in the word.")
        dashes = update_dashes(secret_word, dashes, guess)
    else:
        print("That letter is not in the word.")
    print(dashes)

【讨论】:

    猜你喜欢
    • 2019-08-14
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多