Python,在字母列表中查找多个索引

Tho*_*ong 1 python indexing loops

所以我刚刚开始学习python并为项目创建一个刽子手游戏.我被卡住了.我给你一些背景知识.

我得到了程序来摆脱字母表中的字母,并将它们添加到正在猜测的单词的空白处,但它只会找到第一个字母的索引.所以我要说的是,我试图猜测这个词是安全的.现在让我说我猜字母f.它返回f _ _ _ _ _ _ _而不是f _ _ _ _ _ f _.在我看来,一旦找到列表中的第一个字母实例并在那里打破,for循环就会停止.我需要查找并显示该字母的所有实例.

码:

def makechoice(list)
    # defines the word trying to be guessed as a list of letters
    Global listword
    #defines the amount of blanks in listword as a list "_ "
    global blanks
    #user input to guess a letter
    current = raw_input("Please enter your guess:")
    for a in listword:
        if a == current:
            t = listword.index(a)
            #puts the letter and a blank in place of the unoccupied space if it is a match.
            blanks[t] = str(listword[t]) + " "
Run Code Online (Sandbox Code Playgroud)

不,只是我或不应该循环列表中的所有字母,如果它发现2"f"显示它们两者.请有人帮忙.我做过研究,似乎无法弄清楚我错过了什么.

Tim*_*Tim 5

.index()返回给定字符的第一个索引.如果单词具有多次相同的字符,则它将仅返回第一个索引(除非您明确指定起始偏移量).

当您需要在迭代期间访问索引时,您应该使用enumerate().

for i, x in enumerate(listword):
    # i is the index, x is the character
    if x == current:
        blanks[i] = listword[i] + " "
Run Code Online (Sandbox Code Playgroud)