"Censor" exercise


Hello! I'm working on the exercise "Censor" :

For some reason, my code is only checking the first word in the phrase. It successfully checks the first word, but does not go on to check the rest. Is there something wrong with the way I set up my loop? Or did I screw up the syntax somewhere? Any help much appreciated. Thank you in advance. This is my last exercise in "Practice Makes Perfect." (And it's 1am and I should seriously be in bed. Got carried away again.) Yay!! :blush:

def censor(text,word):
    split_text = text.split()
    censored_phrase = []
    x = 0
    if split_text[x] == word:
        split_text[x] = "*" * len(word)
        x += 1
        x += 1
    return " ".join(censored_phrase)
print censor("this hack is wack hack", "hack")  
#returns "this"


Hello @lizberg,

You have to use a loop to iterate over all of the members of split_list. Because what you're code is doing now is checking if the first item of the split text list is the censor word, adding 1 to x and then returning it. You have to use a while loop to fix this. Hope this clears things out...


That worked perfectly. Thank you @bartholomewallen !!!