简体   繁体   中英

Masking in Python

I'm attempting to arrange the following code so that a digit representing the character place in the word appears below the mask. In other words:

  _ _ _
  1 2 3

Here's my current code:

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

How would I go about this?

A list representation would help you out here:

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

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM