10. censor


#1



def censor(text, word):
    splittext = text.split()
    newtext = []
    
    for i in range(len(splittext)):
        if i == word:
            newtext.append("*" * len(word))
        else:
            splittext.append(i)
    return " ".join(newtext)

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

Need help.


#2

using range() will give you indexes of your splittext list, looking at your code, you don't want this

remove range()


#3

instead of range you should use text


#5

def censor(text,word):
splittext = text.split()
newtext = []

for i in range(len(splittext)):

    if splittext[i] == word:
        newtext.append("*" * len(word))
    else:
        newtext.append(splittext[i])

return " ".join(newtext)

#6

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