Censor_somebody helps


#1

def censor(text,word):
    c = ""
    a = len(word)
    b = text.split()
    for i in b:
        if i == word:
            d = c +  a * "*"
    return d

This is my code and this is the error I get:

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

I don't get why does it only returns *** instead of *** *** *** ?
can somebody helps?


#2

because d gets set *** each time a word in text matches the word which requires to be censored. you can see this:

def censor(text,word):
    c = ""
    a = len(word)
    b = text.split()
    for i in b:
        if i == word:
            d = c +  a * "*"
            print d
    return d
print censor("hey hey hey","hey")

see how d just gets set to *** each time, it just get overwritten every time

there are multiply ways to solve the problem, either by adding (un)censored words to a new empty string/list or replace words in the existing text which requires censoring

See if this helps you further, if you need more help, post an updated version of your code


#3

sorry but can you explain more about the solutions?


#4

you didn't understand anything i explained? That would be sad if i had to explain everything again


#5

That is because of the assignment...

To preserve the current value of d, augment the string, without overwriting it.

d = d + ...

or

d += ...

#6

@mtf , @stetim94
Thanks, I got it.


#7