Censor


#1



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

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


def censor(text, word): 
    split = text.split()
    new_text = ""
    for term in split:
        if term == word:
            term = "*" * len(word)
        new_text += term
        new_text = " ".join(new_text)
    return new_text


#2

why do you use join? .join() is a build in function to join a list into a string, you already have a string. Don't use join.

How are you going to separate the words? You would need to join the words + a space:

new_text += term + " "

but this gives an additional space at the end of your sentence, you would need to slice it off ([:-1])

the alternative is to make new_text into a list, and use .append() and .join()


#3

What you're doing wrong is that you aren't specifying what you want the string to split by. So instead of saying:
split = text.split()
You should instead do something like:
split = text.split(" ")
Which will split it overtime it encounters a space character. Then your code will be perfect...

Regards,
Barry Allen


#4

if you don't provide an argument, split will use a space to separate by default:

print("hey hey hey".split())
print("hey hey hey".split(" "))

both are the same. have you tried to run it with the change you suggested? It still won't work


#5

well that was on the hint..


#6

what was in the hint? to use .join()? Yes, it is. But that is if new_text was a list:

new_text =  []

not if it is a string:

new_text = ""

.join() is for lists.


#7

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

Now it does this mister.


#8

already covered that?


#9

This is how it's done right?

def censor(text, word): 
    split = text.split()
    new_text = ""
    for term in split:
        if term == word:
            term = "*" * len(word)
            new_text += term
            new_text += " "
            new_text = new_text[:-1]
    return new_text

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


#10

wait.. it's done. thank you everyone!!!! it's all in the indentation.. this python better die before it feeds.. :smiley: