Censor


#1



test = "This is many words"


def censor(text, word):
    new_sent = " "
    new_text = text.split()
    for i in new_text:
        if i == word:
            new_sent = new_sent + "*" * 4
        else:
            new_sent = new_sent + i
    print " ".join(new_sent)
    

    

print censor(test, "This")

I am so close! I can't seem to get the sentence to print out correctly. It's either joined like one big long word, or every letter on its own. I guess I'm not fully understanding the .join() function.


#2

Don't multiply the asterix times 4 in fact your for loop i slooping over new_text when it shoudl in fact be looping over your argument word,

for i in word:

and here,

do something like,

new_sent = len(word) * '*'


#3

That's fine, I'll change that.

But the code I have is

test = "This is many words"


def censor(text, word):
    new_sent = ""
    new_text = text.split()
    for i in new_text:
        if i == word:
            new_sent += len(word) * "*"
        else:
            new_sent += i
    print new_sent

    

print censor(test, "This")

and it print out

****ismanywords

But should print out

**** is many words

and I can'y figure out how to get it like that


#4

Hey, try this

def censor(text, word):
new_sent = ""
new_text = text.split()
for i in new_text:
if i == word:
new_sent += len(word) * "*", " "
else:
new_sent += i, " "
print new_sent

print censor(test, "This")

Alternatively:

def censor(text, word):
new_sent = ""
new_text = text.split()
for i in new_text:
if i == word:
new_sent += len(word) * "*"
new_sent += " "
else:
new_sent += i
new_sent += " "
print new_sent

print censor(test, "This")

The important thing is just to get the space in there. Let me know if it works! And thanks, your question and methods inspired me to figure out how to make my code work. Here's mine:

def censor(text,taboo):
censor_list = text.split()
for word in censor_list:
censor_order = censor_list.index(word)
if word == taboo:
censor_len = len(censor_list[censor_order])
censor_list[censor_order] = "*" * censor_len
new_list = " ".join(censor_list)
return new_list

print censor("this hack is wack hack", "hack")


#5

■■■■ I had that with all the right indentations but somehow it got lost :confused:

I hope you get what I mean though


#6

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