在Python中屏蔽

时间:2013-04-05 14:39:53

标签: python mask

我正在尝试安排以下代码,以便在掩码下方显示代表单词中字符位置的数字。换句话说:

  _ _ _
  1 2 3

这是我目前的代码:

secretWord = 'dog'

mask = '_'  * len(secretWord)
for i in range (len(secretWord)):
    if secretWord[i] in correctLetters:
        mask = mask[:i] + secretWord[i] + mask [i+1:]
for letter in mask:
    print (letter, end='')

print ()

我该怎么做?

1 个答案:

答案 0 :(得分:1)

列表表示可以帮到你:

secretWord = list('dog') # ['d', 'o', 'g']

mask = ['_']  * len(secretWord) # not problematic, since strs are immutable
for i,char in enumerate(secretWord):
    if secretWord[i] in correctLetters:
        mask[i] = secretWord[i]
print (''.join(mask))

希望这有帮助