"Censor" exercise


#1



Hello! I'm working on the exercise "Censor" :
https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#

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)
        censored_phrase.append(split_text[x])
        x += 1
    else:
        censored_phrase.append(split_text[x])
        x += 1
    return " ".join(censored_phrase)
    
print censor("this hack is wack hack", "hack")  
#returns "this"


#2

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


#3

That worked perfectly. Thank you @bartholomewallen !!!