def censor(text,word):
    for word in text:
        for i in range(0,len(word)-1):
    return text

what's wrong in this?


You're not changing your input text anyway and every operation is done on word, you simply returning text without censoring the words

even you fix this line...
you'll be checking if characters of text is equal to character of word or not .
By going with this method you'll also censor character of other words in text.


There's a very simple way to do this without using a for loop.

Hint: Try to return the text after replace() the occurrence of word with a string of "*" the length of word. Can be achieved in one line.


took your suggestion it did work but the spaces are being printed as * also..

def censor(text,word):
    for word in text:
    return text

or maybe the whole text is being replaced


(youre almost there)

why need for loop ?

Do not forget to indent your code! :thumbsup:


yes it did work as soon as i removed the for loop but why is it so?
i mean the for loop stated that we move in text and find word and then replace it


Actually by using for loop, You were going through one letter at at time and it replaced everything to * !

To visualize the whole thing...

https://goo.gl/LDuX1X (http://www.pythontutor.com)

also try to visualize without loop. (on that site)

Also we did not needed loop because we were using replace() method.
It'd have censored words(that has to be censored) in one line of code.


its an amazing tool ..thanks loved it
but i dont get the steps after 11 that is step 12 makes the space go as * while i loop only if word is found in the text..
did i have to use break statement or the loop shall not even exist?


In step 12 ,it sees a space character i.e " " and it also replaces it with *!


if i define a string like this..

x = "h ello"

You can see it has one space character!


but why?
it should only replace the code if word is in text right?


Nope,Actually you're thinking that word refers to censored word,right ?
but it is not..
when you use it in for loop as iteration variable..
for word in text:

It no longer have value of censored word but now it gets single letter from text and iterates over and replaces the character of text.

one more thing...as you will think that
('*')*(len(word)) will have same number of * as your censored word then its also not the case.

as now your word (iteration variable) dynamically changes (because its an iteration variable) and it takes one character so it replaces one * each time..
it does same thing with every character of text and thus every letters of text is being replaced by *'s.


got it now
word in text means same as i in text
i was trying to loop the existence of word in the sentence


cheers! :thumbsup:


Just wondering, is using .replace() a cop out/cheating in this excercise? What do you guys think?


I'd suggest you to try other way ,as its not a basic way to solve it.
Try to solve it without replace()?


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