10.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 "*** *** ***".


Expected


Replace this line with your code. 
def censor(text, word):
    censored = " "
    t = text.split()
    if t == word:
        word = "*" * len(word)
        censored.join(t)
    else:
        censored.join(t)
    return censored


#2

t is a list, word is a string (the word we want to censor), a list will never equal a string, look:

def censor(text, word):
    censored = " "
    t = text.split()
    print t, word
    if t == word:
        word = "*" * len(word)
        censored.join(t)
    else:
        censored.join(t)
    return censored

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

this is your code, with some print statement + function call, so you can actually see what your function does


#3

This is what I have now:

def censor(text, word):
    censored = ""
    t = text.split()
    print t, word
    for i in range(len(t)):
        if t[i] == word:
            word = "*" * len(word)
            censored.join(t)
        else:
            censored.join(t)
    return censored

But it returns an empty string. Do you have another piece of advice maybe?


#4

good, now we want t contains the censored string, so when a word requires censoring we need to update t, then after the loop join the list into a string


#5

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