Error in Censor exercise


#1

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:
        x=len(word)
        for letter in word:
            while x>=0:
                censored_word=word.replace(letter, '*')
                new_text=text.replace(word, censored_word)
                x=x-1
            return new_text

#2

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


#3

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

#4

@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.


#5

@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:
        x=len(word)
        while x>=0:
            new_text=text.replace(word, "*" * len(word))
            x -= 1
        return new_text

#6
@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)