Censor


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#


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


Can anyone explain why won't this work? Thank you in advance!


def censor(text, word) :
    orilist = text.split()
    for things in orilist :
        n = 0
        if things == word :
            orilist[n] = '*'*len(word)
            n += 1
        
    return " ".join(orilist)


#2

It's alright I found out what I did wrong. I should not place n inside the loop and also n+= 1 inside the if.

def censor(text, word) :
    orilist = text.split()
    n = 0
    for things in orilist :
        if things == word :
            orilist[n] = '*'*len(word)
        n += 1
        
    return " ".join(orilist)

#3

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