Censor


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#


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


Hello,

I'm having a tough time getting my code to work. I don't understand why my code isn't working, can someone please explain why?


UPDATE : never mind I got it :slight_smile:


def censor(text,word):
    result = ''
    for words2 in text.split():
        if words2 == word:
            result += ('*' * len(words2)) + ' '
        else :
            result += words2
    result = result[::-1]
    return result


#3

I didn't get it....how did you resolve the problem? I am receiving the same error message as yours


#4

The problem was caused because of an extra space in the beginning and end of the return string. Also because I was not adding the spaces correctly. To fix it I added the space to the result in both the if and else statements, then I sliced the result before returning it by removing the first and last space that caused the error. Hope this helps :slight_smile:

def censor(text,word):
    result = ""
    for words2 in text.split():
        if words2 == word:
            result += ("*" * len(words2)) + " "
        else :
            result += words2 + " "
    result = result[0:len(result) -1:]
    return result

#5

Thanks!! It works now!!


#6

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