Censor --> I'm doing what the hint tells me. Code still doesn't work


#1



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


I get the folloing error message:
"Oops, try again. Did you create a function called censor? Your code threw a "global name 'joined' is not defined" error."


As it says in the title - I'm trying to go down the .split - .join route, but can't make it work. Can anybody help? Where's my mistake? Thanks! :slight_smile:


def censor(text, word):
    split = text.split()
    for x in split:
        if x == word:
            for i in x:
                x.replace(i, "*")
    joined == " ".join(split)
    return joined


#2

here:

joined == " ".join(split)

two equal signs means: check if is equal, why would you use that here?


#3

Well this is embarassing... thanks!

But it still doesn't work. Now I get the following error:

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

Any other tips?


#4

one step at a time

here:

x.replace(i, "*")

you do a replace action, but don't store the result of the replace action in a variable, so the replace action is in vein at the moment.

Bigger problem, for both your loops

using a loop like you do, will give you read only values, any changes made it won't persist once the loop has finished running, bit problematic

the problem with replace() is that it allows for a single line solution (replace can just replace all instance of word in text string, not really cool (you are here to learn)

i think you should go back to the drawing board

what you could do is update split with asterisks if x requires censoring

to update items in list, we need indexes. Which function do we know can give us indexes so we can manipulate split?


#6

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