Censor


#1



Practice makes perfect, censor excersise


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


The function returns the correct results, just without spaces. I'm not sure what I'm doing wrong here.


def censor(text, word):
    text = text.split()
    new_text = [ ]
    text2 = ""
    for i in text:
        if i == word:
            i = "*"*len(word)
        text2 = text2+i
    new_text.append(text2)
    return " ".join(new_text)
    
print censor("x y z", "z")


#2

but if you add hey hey hey as argument:

def censor(text, word):
    text = text.split()
    new_text = []
    text2 = ""
    for i in text:
        if i == word:
            i = "*"*len(word)
        text2 = text2+i
    new_text.append(text2)
    return " ".join(new_text)
    
print censor("hey hey hey", "hey")

you can see the wrong thing is printed.

i would remove text2, append censored and uncensored words (use if/else) to new_text and join that together


#3

Thank you so much! This was driving me insane!!


#4