首页 > 解决方案 > Hangman Python 相同字符的两个实例

问题描述

elif guess in word:
    already_guessed.extend([guess])
    index = word.find(guess)
    word = word[:index] + "_" + word [index +1:]
    display = display[:index] + guess + display[index +1:]
    print (display + "\n")

要猜的词是女王,如果我猜 e 它不会填满 e 的两个实例。你会如何解决这个问题?

标签: python

解决方案


扩展zvone的答案:

elif guess in word:
    already_guessed.extend([guess])
    for index in range(len(word)):
        if word[index] == guess:
            word = word[:index] + "_" + word [index +1:]
            display = display[:index] + guess + display[index +1:]
    print (display + "\n")

推荐阅读