10. Censor


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#


I don't understand why my loop doesn't replace the word with asterisks.


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


#2

Hint**
Suppose my input text is ...
"hey, hey and teds"
When split method breaks it into pieces it will broken like this..
( I meant to say that split method breaks string at spaces)
You will get..
['hey,', 'hey', 'and', 'teds']
Can you see one of the hey is "hey," ?


#3

Thanks greentreemee, I should include my latest error with this code though. The error I'm seeing is:
Your function fails on censor("hey hey hey","hey"). It returns "hey hey hey" when it should return "*** *** ***".

So it's failing even without a comma within one of the hey's.

Or does your hint address that too and I'm just missing it? lol


#4

here:

for i in text:

i will contain read only values from list, any changes made to i won't persist


#5

It(hint) was just for split method, The error now you're getting is mentioned by @stetim94. (Tho it existed from beginning. )


#6

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