Practice Makes Perfect 10/15


#1

Hey everyone, I'm having some trouble with my code for this assignment. I think I've ruled out a few issues and narrowed it down to my .appending, but I'm not sure. I get this error:

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

My code is below:

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

#2

Nevermind, I am dumb. For anyone with a similar problem, the issue was a typo here:

for item in text.split(text):

Leaving the brackets empty was the solution.


#3

Thanks a lot. I was having similar issues.


#4
> def censor(text, word):
>     input1 = text.split(" ")
>     output = []
>     for x in input1:
>         if x.lower() == word.lower():
>             output.append("*" * len(x))
>         else:
>             output.append(x)
>     return " ".join(output)

you need to state what you are using as your "split" trigger. (in this case, the string " ")