Censor


#1



def censor(text,word):
    asterisks = '*' * len(word)
    t = text.split()
    for i in range(len(t)):
        if t[i] == word:
            t[i] = asterisks
    text = ' '.join(t)
    return text


#2

.join() is used to convert a list to a string. new_text is already a string so will raise an exception. String type does not have a join() method.

Let new_text be initialized as an empty list

new_text = []

then append to the list

    new_text.append(term)
else:
    new_text.append("*" * len(word))

Now the .join() method can be used to return a string

return " ".join(new_text)

#3

Thank you for your reply!
I just realized that I posed a wrong python script written by others, and I used it to test. Now I`ve replaced it with the one I wrote.

def censor(text,word):
    asterisks = '*' * len(word)
    t = text.split()
    for i in range(len(t)):
        if t[i] == word:
            t[i] = asterisks
    text = ' '.join(t)
    return text

#4