Censor


#1

This is my code:

def censor(text,word):
    text=raw_input('Enter a text:').split()
    cens=[]
    for i in text:
        if i==word:
            cens.append("*"*len(i))
        else:
            cens.append(i)
    return ' '.join(cens)

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

But, I checked it in IDE. It worked.


#2

Format your code using this sign (</>) On the console you used to post your code please.


#3

i wouldn't use a raw_input, remove this:

text=raw_input('Enter a text:').split()

you can pass different sentences and what to censor when you call the function.

then your for loop, text is a string, so for i in text: will simply give you each letter:

for i in text:
   print i

not really working. You might want to think of a different solution to overcome this problem. One more thing, your join:

return ' '.join(cens)

join will add a space between the different list elements when you do '', using ' ' will put an additional in each character, and a additional space between the words (so now you have two spaces between your words)


#4

And your code might be failing because they never asked you to ask a user for input. And you don't need an empty list for this exercise just an empty string. after the empty string you are trying to censor i.e(text = ""). Just go straight to your for loop...
Also before you go to your if statement you need to add an asterix to your empty string for each word in the text that you are trying to censor i.e(text += " * ")

Compile all that first then show me what you come up with.


#5

If you would make text a string, then how are you going to make sure there are spaces between the words? I would use a list, and then .join() to convert it into a string


#6

I was using the name of his variable...

This is what i meant for the beginning of his code

def censor(text, word):
    censored_word = ""
    
    for c in word:
        censored_word += "*"

Maybe the way I explained it was abit confusing


#7

okay, but this code still has serious flaws. Now you just replace each word with one asterix (*), i hope you thought this through.


#8

I didn't want to explain all the flaws in his code at one go, because I didn't want to confuse him. hence the reason i said "Compile all that first then show me what you come up with."


#11

I changed the code. It works now. :grinning: