Error in Censor exercise


Can anyone help me understand were I went wrong?
I know that exists a compact code, that solves this exercise.
But I only want to know were my line of thought went wrong.

It loops forever.

def censor(text, word):
    if word in text:
        for letter in word:
            while x>=0:
                censored_word=word.replace(letter, '*')
                new_text=text.replace(word, censored_word)
            return new_text


This has too much indentation. Move it four spaces to the left.

def censor(text, word):
    return text.replace(word, "*" * len(word))
print censor("this hack is wack hack", "hack")


@dheart As was discussed in another topic, replace() does not need to be in a loop. It is ‘greedy’ and therefore replaces everything in one pass. The above demonstrates that.


@dheart, if you use the example posted by @seraphas your code will works: change the text.replace(word, censor_word) for text.replace(word, "*" * len(word))

If you do that you won’t need the variables letter and censored_word also:

def censor(text, word):
    if word in text:
        while x>=0:
            new_text=text.replace(word, "*" * len(word))
            x -= 1
        return new_text

@dheart here's what I did

 def censor(text, word):
    lst = text.split()
    for index, item in enumerate(lst):
        if word == item:
            fill = "*" * len(item)
            lst[index] = fill
    return " ".join(lst)