def censor(text,word):
    censored = text.split()
    for char in censored:
        if char == word:
            return('*' * len(char)
    new_text = " ".join(censored)

Hey, this says that the equal sign in new_text is invalid? What am I doing wrong?


There is a syntax error in this line, not the one with the indicator. That is where parsing stopped so look backwards from that point to discover the error.

The error is return( which should just be return ..., but then that results in a logic error and an incorrect return value (a string of stars).

Start with an empty list before the loop, and append to that list either the words that are not censored, or the star string.


Ok now I have the following error message:

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

this is my updates code:

def censor(text, word):
    x = text.split(" ")
    for i in text:
       for j in word:
           if i == j:
               j = "*"
               return ("*" * len(x))
    return text

print censor("c'est la merde", "merde")

I dont understand why it is printing *** altough it should print c'est la *****


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