Error with Else statement in Censored


#1



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

I've gotten something that, as I run through it line by line, should work. However, I keep getting two error messages. In the console, I see:

File "python", line 7
else:
^
SyntaxError: invalid syntax

Meanwhile, in the editor, I see this:

Oops, try again. Did you create a function called censor? Your code threw a "global name 'censor' is not defined" error.


def censor(text, word):
    words_arr = text.split()
    final = ''
    for check_word in words_arr:
        if(check_word == word):
            final.join(" " + "*" * (len(check_word))
        else:
            final.join(check_word)
    return final
    
censor("I like apples and oranges and bananas", "and")

Admittedly, there's a pretty good chance that this is just my bonehead forgetting something simple about conditionals, but can someone please help me because I can't figure out for the life of me what it is.


#2

You haven't close a paranthese after final.join() function.

final.join(" " + "*" * (len(check_word))

should be

final.join(" " + "*" * (len(check_word)))

Also, it is not mandatory to use parentheses in if statement.


#3

Additionally, the .join statement can simply be:

final.join(" " + "*" * len(check_word))

No need for parentheses outside the len method.


#4

Thanks to both of y'all!! That took care of that issue.

That said, if anybody's looking here for a solution, know that even after making the changes above, it still didn't quite work, but I was able to figure it out from here.