CENSOR, what's wrong with my code here



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):
    new = text.split()
    for words in new:
        if words == word:
            words = ["*" * len(word)]
    return text


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


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:


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?


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


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