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

I want my function to return "*** *** ***" instead of "hey hey hey", hey")

def censor(text, word):
    x = str(text)
    x = str.split(x)
    if word in x:
        word = "*" * (len(word))
        x = " ".join(x)
        return x


could you explain the logic behind your code so i can correct where you go wrong?

you will need a for loop to see if the word requires censoring or not.

There are two common approaches, either use range() which gives us indexes so we can manipulate the list (update words that need censoring)

or make a new list and append to this new list, using if/else to determine if we need to append asterisks or just the current word in sentence

also, here:

x = str.split(x)

the string you want to split is x, currently you split str (which seems to be a undefined variable)


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