Censor


#1



def censor(text,word):
    text = text.split()
    new = []
    s = ""
    for word in text:
        m = "*" * len(word)
        text = str(text).replace(word,m)
    new.append(text)
    text = " ".join(new)
    return text


Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "['', '', '']" when it should return " *** ***".


I am confused with the result.What's wrong with it? Thank you for helping me


Replace this line with your code.


#2

using string.replace() allows for a single line solution, kind of dull

try using if/else, append to new either asterisks or the word (depending on if it needs censoring, that is what if/else is for), then join the list into a string and return it

don't use .replace()


#3

Let me try thank you


#4

def censor(text,word):
    text.split()
    new = []
    for item in text:
        if item == word:
            item == "*" * len(word)
            new.append(item)
        else:
            new.append(item)
    text = "".join(new)
    return text

Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "hey hey hey" when it should return "*** *** ***".
why I can't replace hey with *?


#5

you no longer store the split string anywhere (a variable) like you did earlier?

here:

item == "*" * len(word)

why use two equal signs? Which means: check if equal


#6

I got it!Thank you again


#7