Censor?


#1



What Am I doing wrong?

def censor(text, word):
    new_censor = text.split()
    new_word = []
    for i in new_censor:
        if i == word:
            i = "*" * len(word)
            new_word.append(i)
            result = "* ".join(new_word) 
    return result


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


Replace this line with your code.


#2

this is good:

def censor(text, word):
    new_censor = text.split()
    new_word = []
    for i in new_censor:
        if i == word:
            i = "*" * len(word)
            new_word.append(i)

but what if a word (i) doesn't equal word? You will need to append i to new_word

then after the loop join the list into a string, the character between the quotation marks is what join uses to join the string (between the different elements of the list)

if you need more help, post an updated version of your code


#3

Thanx!
This is my new code:

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

It works!


#4