About censor, kinda stuck


#1

Hi, I'm struggling with censor section currently.
After searching, i figured that i can use list.replace(a,b) to make things easier,
but since i'm learning, i would like to use str.split and ' '.join to make this code work.
This might be because of my weak understanding about how loop works, but
I'm keep getting

Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "" when it should return " *** ***".
message. (It would only print first word)...

i would be grateful if someone tell me why i'm getting only first word as ****, or how i can fix this.


def censor(text,word):
    asterisk = len(word) * '*'
    words = str.split(text)
    for w in words:
        if w == word:
            w = asterisk
        return w
        print words[w]
    result = " ".join(words)
    return result
    
print censor('buff buff buff','buff')


#2

You return before the function has finished

Another problem is that you are discarding your results. (You assign to w which has no effect on words, w is just a variable)


#3

Thanks! I keep getting confused where i should return...


#4

When it's done.