10.censor


#1



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


Hi everyone, I'm stuck at this exercise. It says: Your function fails on censor("hey hey hey","hey"). It returns "***" when it should return
"*** *** ***"


Can anyone see the problem? Thanks!


def censor(text,word):
    string_list = text.split()
    for item in string_list:
        if item == word:
            return "*" * len(item)
    new_text = " ".join(string_list)
    return new_text


#2

a function ends the moment a return keyword is reached, if this happens in a loop, the loop simply breaks, so your function ends:

return "*" * len(item)

at this line


#3

So how can I fix it?

thanks


#4

not use return inside the for loop.

How did you think you where to censor the words in text?


#5

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