Where is my censor code wrong? Censor Practice M. P


I get an error message saying:

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

def censor (words, profanity):
a = "*" * len(profanity)
words = words.split(" ")
for i in range(len(profanity)):
    if profanity == words[i]:
        words[i] = a
return " ".join(words)```


Hi, @cloudcoder15482 ,

Are you sure that len(profanity) is really the appropriate number of times to iterate through the words list here? ...

for i in range(len(profanity)):


Thank you, i found out i wrote len(profanity) instead of len(words) in line 4 :smiley: @appylpye


You can use this simple code

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


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