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


#1

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)```

#2

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)):

#4

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


#5

You can use this simple code

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


#6

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