Censor


#1



hello,guys,I got a problem,but I can't figure out.hope you guys help me . thanks a lot.


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


def censor(text,word):
    text=text.split()
    print text
    new_list=""
    for string in text:
        if string==word:
            new_list=new_list+(len(word)* "*")+" "
        else:
            new_list=new_list+string
       
    return "".join(list(new_list))
    
print censor("hey hey hey","hey")


#2

there is an additional space at the end of your sentence, you could slice it off? Learned string slicing


#3

I changed my code and the result is right now. thank you anyway!
def censor(text,word):
text=text.split()
print text
new_list=[]
for string in text:
if string==word:
new_list.append(len(word)* "*")
else:
new_list.append(string)

return " ".join(list(new_list))

print censor("hey hey hey","hey")


#4

that is even better, i didn't want to suggest it given you might find it difficult


#5

A post was split to a new topic: Censor


#6

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