Censor - weird error


#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 "the password is **** *** *** ***" when it should return " *** *** ***".


I am confused - why does the error say it returns "the password is **** *** *** ***" when the text checked does not mention anything regarding any of this text?? It is just "hey hey hey" there as text

Here is my code, for reference:

result = []
def censor(text, word):
    words = text.split()
    for w in words:
        if w == word:
            result.append("*" * len(w))
        else:
            result.append(w)
    return ' '.join(result)


#2

That's a very strange error. Your code appears to be correct and, like you said, says nothing about a password or any of that. Have you tried reloading the page?


#3

Yup - just tried again. Also tried changing some info like the names of variables, copying than reseting the code than pasting... does not work


#4

I have no idea why it was throwing that error, perhaps someone else can shed some light on that, but I did notice one problem with your function. The result list is stored outside of the function when it should be inside the function since it's part of the process of censoring.

If you move result = [] inside the function then it should resolve the error.


#5

Wow - thanks! It worked! Never thought of that since I had it defined before the function in every exercise so far.


#6

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