Censored


#1

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

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


#2

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.


#3

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 = "*"
               i.join(x)
               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 *****


#4

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