10. censor


#1

Hi, I'm very new to coding and would really appreciate a bit of help on the censor task. My code currently returns the error message "Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "hey hey hey" when it should return "*** *** ***"."

Here is what I have so far...

def censor(text, word):
    text.split()
    for words in text:
        if words == word:
            text.replace(text_word, "*" * len(text_word))
    return "".join(text)

#2

i added a print statement to your code:

def censor(text, word):
    text.split()
    for words in text:
        print words, "does not equal", word
        if words == word:
            text.replace(text_word, "*" * len(text_word))
    return "".join(text)

print censor('our cat eats', 'cat')

as you can see, words are letters now, it will never equal word, you use text.split(), but you don't store it anywhere, and you don't tell the split where to split, of you where to do something like:

for words in text.split(" "):

you would loop over the words, rather then each character


#3

Thank you! I was able to fix it and get it to work by changing for loop to go over words instead of characters.