Weird error in Censor


#1


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

I get the expected output on the terminal, however I get a very strange error message.

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

I am unsure where this additional text is coming from. Also, how messed up is my code? Is there a better way to go about this? Thank you very much in advance!


lst = []
def censor(text,word):
    n = text.split()
    for x in n:
        if x != word:
            lst.append(x)
        else:
            lst.append("*"*len(word))
    return ' '.join(lst)

print censor("hey hey hey","hey")


#2

the lst[] is outside the function... hence it is returning garbage...

hope this helps...

Thanks


#3

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