I'm trying to do the censor function, but it wont return to me the whole text, only the word that is censored out. Why doesn't it loop through all the words in the text? What can I do to this code to make it work?


def censor(text,word):
for c in text:
    if c==word:
return c 
return text

when I run the code it gives me this error message.

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

Replace this line with your code.


Lots of indentation issues.

Consider using a variable other than text so the original input is preserved.

words = text.split()

Create an empty list to store your intermediate results.

result = []
for w in words:
    if w ==  word:
        result.append( ... )
return " ".join(result)


Create a new list that appends c. After that, you join the new list.
This is what i used:

def censor(text,word):
    texspl = text.split()
    texnew = []
    for a in texspl:
        if word == a:
            a = "*" * len(word)
    return " ".join(texnew)


Censor can be done 4 lines:

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


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