10. Censor



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)
            i = i
    return ' '.join(text)


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," ?


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



for i in text:

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


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


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