10. Censor: why doesn't this work?


#1
def censor(text,word):
    text_list= text.split()
    new = []
    print (text_list)
    for c in text_list:
        if c in word:
            new.append("*") 
        else:
            new.append(c)
        print (c)
    "".join(new)
    return new
    
censor("test","es")

Can anyone tell me why this doesn't work? I don't understand why not? I split me text in a new variable and for every element in the list I check if it matches any substring of word. If so, replace with a * and append. If not append original character. The result is:

Your function fails on censor("hey hey hey","hey"). It returns "['', '', '*']" when it should return "*** *** ***".

It seems like my text is not split up but looking at the examples on the forum here makes me believe I have made no mistake. Apparently I have. Help?


#3

Noone understands? :frowning:


#4

I'm pretty new to all this but I don't understand the logic of your if statement at all. If it works the way it reads it looks like you're saying if the letter you've looped to in text_list exists in the word variable at all it'll print a *. So if you had ("fantastic", "fame") as your input it'd come out **ntastic if everything else worked.


#5

What I am trying to do is split the text into a list of characters, check if any of those characters match a character in the word. And if so replace it with an asteriks. If not keep it the way it is. Then append. But as I'm reading your reply I am starting to realize it is supposed to replace words, not characters. And the fact that you don't understand my code at all says a lot too....I need to look into this again and start from scratch I think. Thanks.

Edit: ok, this works a lot better so far:

def censor(text,word):
    text= text.split()
    new = []
    print (text)
    for c in text:
        if c == word:
            new.append(len(c) * "*") 
        else:
            new.append(c)
        print (c)
    ' '.join(new)
    return (new)

The only thing is that my list is never joined into a string, it stays a list. Anyone got an idea?


#6

There's fully working solution.
Regards
Adam

def censor(text,word):
/tab/text = text
/tab/for txt in range(0,len(text)):
/tab//tab/if text[txt] == word:
/tab//tab//tab/text[txt] = len(text[txt]) * "*"
/tab/print ' '.join(text)
/tab/return ' '.join(text)
censor("hey hey hey", "hey")

Ps. /tab/'s are Indents.


#7

Thanks. I had already created a working solution:

def censor(text,word):
    text= text.split()
    new = []
    for c in text:
        if c == word:
            new.append(len(c) * "*") 
        else:
            new.append(c)
    return (' '.join(new))

#8

That's good idea.
i can't imagine solution by using list.append().

the following is my case, but yours is more simple (i like your code more than mine, lol)
thx. :slightly_smiling:


#9

Thanks :smiley: Took me some time though.


#10

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