Please help - my code doesn't print the asterix!


#1

Can anyone set me in the right direction for figuring out why my code isn't printing the asterix? I've been working on this so long now I can't see anything new. My hunch is that I'm not actually changing the matching word to the asterix. Please help! Many thanks.


def censor(text, word):

    stars = "*" * len(word)
    split_words = text.split()
    for each in split_words:
        if each == word:
            split_words[each] = stars  
    censored_text = " ".join(split_words)
    return censored_text

#2

By your logic this is the corrected code

def censor(text, word):
    stars = "*" * len(word)
    split_words = text.split()
    for each in split_words:
        if each == word:
            #this line is all that changed
            split_words[split_words.index(each)] = stars  
    censored_text = " ".join(split_words)
    return censored_text
print censor("they know know know", "know")

#3

Thank you so much! Is the change related to the fact that the list contains strings and not integers? I have never accessed elements in a list this way while looping. I've always used list[i] only, for example. Again, thank you for your help!