10. censor


#1



hi, can someone tell me what's wrong with my code?

https://www.codecademy.com/en/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 " *** ***".

def censor(text, word):
    texts = text.split()
    result = ''
    for i in texts:
        if i == word:
            result= result + ('*'*len(i))
        else:
            result= result + i
    ''.join(text)

    return result

I don't know how to add the space between the '*'

thank you


#2

you can add a space after a (un)censored word:

result= result + ('*'*len(i)) + " "

you can do the same for a uncensored word, but this will give an additional space at the end, which you then need to slice of ([:-1]) before returning the string. The other thing you can do is make result a list, use append() to append the items to the list, and then use " ".join() to join the list into a string


#3

is it the right code?I'm confuse

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

def censor(text, word):
    texts = text.split()
    result = []
    for i in texts:
        if i == word:
            result.append('*'*(len(word)))
        else:
            result.append(i)
    ' '.join(result)
    return result

#4

you need to store the join you do:

result = ' '.join(result)

or you can return the result of the join directly. Currently, you don't store the result of the join somewhere


#5

ok I got it. it's working now. thank you so much!


#7