10. censor


Can anybody help me figure out whats wrong with the following code, though the code is doing its work but still an error is coming

 def censor(text,word):
    result = ''
    text_list = text.split()
    print text
    print text_list
    for worm in text_list:
        if worm == word:
            result += '*' * len(worm) + " "
            result += worm
    return "".join(result)

censor('our cat eats', 'cat')


Hi @peeyushchawla ,

Note the message that Codecademy displays ...

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

Your function returns an extra space at the end of the string, and that is why Codecademy does not like it.

You are passing result to the join method here, when result refers to a string ...

return "".join(result)

However, join should be passed an argument consisting of a list of words and strings of asterisks, in this exercise. Start out the function with ...

result = []

Then, instead of this ...

result += '*' * len(worm) + " "

... and this ...

result += worm

... just append each new string to the result list, with ...

result.append('*' * len(worm))

... and ...


Finally, use join to combine the strings in the result list together, with a space in between each ...

return " ".join(result)


Hey, if you're getting that message like i was you just need to return slightly different.

like this:
return alt_text[:-1]
This simply removes the last char off the string.

Now i know this isn't the shortest way of writing this program but it took me ages to get right and i'm so proud of it i feel i need to share it :smile:

def censor(text, word):
    text = text.split()
    count = 0
    alt_text = ""
    for words in text:
        if words == word:
            word_len = len(word)
            ast = ""
            a = 1
            while a <= word_len:
                ast += "*"
                a += 1
            alt_text += ast + " "
            alt_text += words + " "

            count += 1

    return alt_text[:-1]


Thank you, yeah even I did it the way you told but I wanted to know if we could solve it this way

def censor(text,word):
    a = text.split()
    c = []
    for b in a:
        if b != word:
            c.append(" ")
            c.append("*" * len(b))
            c.append(" ")
    d= "".join(c)
    e = d.strip() #You can use strip() to remove the excess space at the end
    return e


Simple solutuon:


I have a shorter way for this :smiley:


Here is my solution.

def censor(text, word):
    return text.replace(word, '*'*len(word))


I have a slightly different way of doing this. Take a look, I tried making it simple to understand, especially for NOOBs like me :wink:


The only problem with .replace is that it will literally replace any instance where the characters appear. For example, if the sentence is "This is fun", then to replace the word "is" with "isn't" would output the following result:

"Thisn't isn't fun"


it's very helpful for me. thanks