Censor


#1

def censor(text,word):
    for word in text:
        for i in range(0,len(word)-1):
            word=word.replace(word[i],'*')
    return text

what's wrong in this?


#2

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...
word=word.replace(word[i],'*')
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.


#3

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.


#4

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

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

or maybe the whole text is being replaced
help?


#5

(youre almost there)

hint**
why need for loop ?

Do not forget to indent your code! :thumbsup:


#6

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


#7

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.


#8

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?


#9

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

example..

if i define a string like this..

x = "h ello"

You can see it has one space character!


#10

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


#11

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.


#12

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


#13

Exactly!
cheers! :thumbsup:


#14

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


#15

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


#16

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