Censor


#1

my code works well and the error still pop up, i have no idea, plz help me thank you!

def censor(text,word):
new = []
for i in text.split():
if i == word:
i = '*' * len(word)
new.append(i)
elif i != word:
new.append(i)
new_result = ' '.join(new)
print new_result

print censor('hey hey hello','hello')


#2

i just added the 'return new_result' under the "new_result = ' '.join(new)" so my code is like:

def censor(text,word):
new = []
for i in text.split():
if i == word:
i = '*' * len(word)
new.append(i)
elif i != word:
new.append(i)
new_result = ' '.join(new)
return new_result
print new_result
print censor('hey hey hey','hey')

and it works well.
but i still can't figure out why this return is that important, can i just print it out without return it?


#4

Hi netslsyer97076,
Thank you for posting your code. I was trying to do this task and felt I have the idea but missing how to organise them in a code. So, I have changed your code a bit and it worked very well. please look at the code below:

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

print censor('hey hey hello','hello')

#5

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