Censor


#1



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


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


What is wrong with this code? I'm very confused.


def censor(text,word):
    text1 = text.split()
    for i in text1:
        if i == word:
            text1.remove(word)
            text1.append("*" * len(word))
        text2 = " ".join(text1)
    return text2


#3

Could we get an explanation of this code, please?

The answer is great and all but without knowing why this works there's nothing being learned.

From the "thanks for contributing"

Welcome to Codecademy Discuss — thanks for contributing!

Does your reply improve the conversation in some way?

Be kind to your fellow community members.

Please do not post the answer without an explanation. Code only answers will be removed. Help the user learn to troubleshoot.

#4

You could also try it with the replace function. I know its not an answer to your question but a little hint


#5

I had a similar issue. I finally tried using the insert function to insert the asterisks into the list. Then I removed the string at the index of i.

if i == word:
newstr.insert(newstr.index(i), "*" * len(word))
newstr.remove(i)
text2= " ".join(newstr)


#6

instead of running for loop in comparison with 'word' you better do by its index. just a suggestion.

ex: for i in range(len(text1)):
try identifying word by its index and retun " ".join(text1)


#7

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