10. Censor


#1




I want to solve this task thorugh next steps: replace the word on ***.
But my code replace only the last word


Replace this line with your code. 
def censor (text,word):
    text = text.split()
    word_len=len(word)
    for i in text:
        if word in text:
            word1 = word_len * '*'
            text.append(word1)
            text.remove(word)
            return ' '.join(text)
print censor('Hello guys guys','guys')


#2

What do you think happens when you remove from a list that you're currently looping through? Would the loop be entirely unaffected? How many times would your loop need to run to do its job? How many iterations do you think it does? How many does it do? (add a print in it to find out)


#4

def censor(text,word):
    return text.replace(word,"*" * len(word))

Share a simple method.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.