I have no idea what's wrong with my code. hope someone can help.

def censor(text,word):
    new_word = text.split(" ")
    for i in new_word:
        if i == word:
            i = "*" * len(i)
            return " ".join(new_word)
print censor("hey hey hey", "hey")


i contains read only values from the list, any changes made to it don't persist

Also, a return keyword will cause a function end, in order for the function to end when a return keyword is reached in a loop, the loop will break

so, your loop breaks in its first iteration, this is not what you want


intendation of return is wrong, it will return for each loop.
This can be done in 2 lines really, you need to use len() inside a str.replace(). This and your "Hint" should solve it.


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