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


#1



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


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


def censor(text,word):
    censoredtext = ""
    splittext = text.split()
    if word in [splittext]:
        for item in word:
            censoredtext = "*" + censoredtext
    else:
        censoredtext = word + censoredtext
    return censoredtext


#2

I don't understand the problem. It really returns "hey" for censor("hey hey hey","hey").

Do you need help? Please be more descriptive, explain what you do not understand, basically be nice.


Why it returns "hey"? Ok, take a look, this is how your function works for these parameters:

text = "hey hey hey"
word = "hey"

censoredtext = ""
splittext = ['hey', 'hey', 'hey']

# condition of if
"hey" in [['hey', 'hey', 'hey']] = False

# condition is false so we are in the else block
censoredText = "hey" + ""

return "hey"

#3

how do I read "hey" in ['hey','hey','hey']?


#4

how do I read "hey" in ['hey','hey','hey']?

Is "hey" inside ['hey','hey','hey']?


And in this case... it is. But there is a big difference between ['hey','hey','hey'] and [['hey','hey','hey']].


#5

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