10. censor


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#


It seems I am not replacing "word" with the star string, but instead I change every other word but it keeps the length of the "go" string. I'm not sure why this is, because I would think it looks for "word" and tries to replace that.
When i switch the if/else statement however, it doesn't replace anything anymore.

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


def censor(text, word):
    censored = []
    for i in text.split():
        if i == word:
            censored.append("*"*len(word))
        else:
            censored.append(word)
    return " ".join(censored)


#2

the problem is here:

else:
    censored.append(word)

else handles words which don't require censoring in your text string, yet you append word (the word you want to censor)


#3

Okay, I changed it to this:

def censor(text, word):
censored = []
for i in text.split():
if i == word:
censored.append(""len(word))
else:
censored.append(i)
return " ".join(censored)

It's censoring the right word now, but only once it seems. this is the error:
Your function fails on censor("hey hey hey","hey"). It returns "*** " when it should return " *** *** ***".

How is it returning only one "hey"?


#4

a function ends the moment a return keyword is reached. If the return keyword is reached in the loop, the loop will break

You had return placed outside the loop, so the whole loop could run, but now you placed it inside, so the loop breaks in the first iteration


#5

Oh my god duh. Thanks a lot :slight_smile:


#6