Help! Again.....Practise Makes Perfect: 10. censor


#1



Practise Makes Perfect: 10. censor


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


It shouldn't be converting the spaces to asterisks, unless it's converting the whole string 'text'??


def censor(text, word):
    for word in text:
        text = text.replace(word, len(word) * '*')
    return text
    print text


#2

when using replace, why do you even have a loop? replace() does all the work for you

using replace your function content can be a single line

I personally i think its a boring while in the learning process


#3

NICE, thank you. So why does having the loop mess up the code?! I get it was irrelevant, but why did it break the code..?


#4

because here:

for word in text:

your loop iterator word overwrites your function parameter word

given you are looping over a string, it will give each letter:

h
e
y
 
h
e
y
 
h
e
y

which seem to cause problems


#5

Haha! Of course, just because I specify 'word' there that doesn't mean anything in a for loop does it...Thanks so much


#6

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