Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "hey hey hey" when it should return "*** *** ***".

why my code doesnt working.......?

def censor(text,word):
    new = ''
    for a in text:
        if a == word:
            a = "*" * len(word)
            new += a
            new += a
    return new


you split text into a list, but don't store this list into a variable? So text is still a string


Text remains a string type if doesn't store it in a variable. And then would you concatenate strings?


When you make for loop, you go through string, counting LETTERS, not words. How supposted to be equal any letter with second input WORD? Its like 'a' == 'hack'.
So i would recommend you:
1) make empty LIST as 'new' variable , not string. new = []
2) make this: text = text.split() also, it will connect split result to variable.
3) new += a When you add your operation into list better use append method here, because += will add letters into list instead words. new.append(' * ' * len(word)) ( i made extra spaces to show how i separated this, you should use standard synthax)


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.