CENSOR, what's wrong with my code here


#1


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

What's wrong with my code?

I think I am doing right but still it's not working can anyone please tell me where I am wrong?
Thanks is advance..


def censor(text, word):
    text.lower()
    new = text.split()
    for words in new:
        if words == word:
            words = ["*" * len(word)]
        text.join(words)
    return text


#2
"separator_string".join(list_name)

The separator string may be an empty string, a space, a comma and space, or any other string.


#3

Now your function does nothing. It takes text as the parameter and returns this value without changing it.

join method does not mutate the original value, it returns a new value. So if you want to make a change to text you have to assign returned value:

text = text.join(words)

But this means that you want to join together elements of words and separate them by text.

print "-".join("abcd")
=> a-b-c-d

Still, your function is not correct, but maybe now you will be able to find other errors without our help. At least try :slight_smile:


#4

I corrected and passed this,

def censor(text, word):
new = text.split()
for i in range(len(new)):
if word == new[i]:
new[i] = "*" * len(word)
text = " ".join(new)
return text

but still could you please explain me , why we put inverted commas(" ") before .join(new). What does that do?
Thanks.


#5

This line:

text = " ".join(new)

says join all elements of sequence new and separate them by a space.

" " is a string containing a space.

print " ".join(['a', 'b', 'c']) # separate by a space
=> a b c
print "".join(['a', 'b', 'c']) # don't separate (empty string)
=> abc
print "-".join(['a', 'b', 'c']) # separate by a hyphen
=> a-b-c

#6

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