1

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

  _ _ _
  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 ()

我该怎么办?

4

1 回答 1

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))

希望这可以帮助

于 2013-04-05T14:47:09.743 回答