Censor


#1

This is my code:

def censor(text, word):
final_text = text.split()
cword = ''
for item in final_text:
if item == word:
cword += len(item) * "*"

return final_text

I couldn't replace the 'word' with 'cword' inside final_text I think it's the only thing missing. How should I do it?


#2

After deciding whether the word should remain the same or not, assign it back to the same position in the list


#3

How would you do it?


#4

My code is looking like this now:

def censor(text, word):
final_text = text.split()
cword = ''
for item in final_text:
if item == word:
cword += len(item) * "*" + " "
else:
cword += item + " "
return cword

and it is working, but codecademy doesn't recognize it, it keeps appearing that message:
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** " when it should return "*** *** ***".


#5

Well. It's not the same as it says it should be. Have you compared them and identified the difference? There are about 10 characters there to compare, gogo.


#6

I saw the space between the last * and the ", but if I remove the space it would return like "*********"


#7

I used the .join() function to avoid the spaces issue. You have to have it as a list though to do that I think.


#8

My way works just fine, but I guess the point of the exercise is to do what you did. I'll have to rewrite.


#9


Yes, I also had much trial and error, finally got the answer. I just couldn't get past the replace point. Brain freeze I guess.


#10

Thank you,, I was about to throw my computer of the window


#11

Probably the easiest solution:

def censor(text,word):
text = text.replace(word, "*" * len(word))
return text


#12

The point of the exercise is to think about what actions you need to perform to get the desired results, and what data structures there are to support those actions.

This is one of those cases where it's all about the journey, not the destination

For example, you've got the problem that you're adding a space after each word, but there should only be spaces between them. What's the difference between those two? The difference is that the last word should not get a space after it, so you'd need to test for that, for example by counting how many words you've processed and comparing to how many there are.

Computers operate in the same universe as us, the same laws of physics/logic apply, operations that you can think up can also be implemented by the computer as long as you describe them sufficiently


#13

Seriously I like how you are breaking down line by line. I'm going to start doing this, even if it's wrong. Really helps understand what a user is trying to do.


#14

Yes!...getting stuck is the hardest part; not knowing why something is the right answer is just as bad. Best wishes for continued coding :slight_smile:


#15

Thanks. I also hope people will point out if there's a better way (which there almost always seems to be!)


#16

Can I ask the function of "list" under line10? Since I get stick without it. Many thanks!


#17

It brings other parts of the statement together, the if statement tells it to look for a word -in the text- and if it's equal to the one entered, it should replace with whatever necessary, the else handles the case of words in the text not equal to the one you're looking for and you want them to remain the same hence just add them as they are. So without it, no complete statement and that's why you get stuck.


#18

def censor(text, word):
return text.replace(word, ""len(word))


#19

As another poster already pointed out, without line 10, any items that did not match, would not be copied.
So you might end up with an incomplete list, for purposes of what the exercise is asking for.


#20

Hi there,

I solved this by doing the following:

def censor(text, word):                           #define a function
    x = len(word)                                     #find the length of the given word for use later
    for i in text:                                     #iterate through the split text
        text = text.replace(word, "*" * x)     #in text, replace word with a '*' multiplied by variable x
    return text                                        #return the text

I am an amateur myself but this is the simplest way I have found to solve this problem. I could alternately avoid storing the variable x and simply writing:

 text = text.replace(word, "*" * len(word))

but to understand it properly I think its simpler to store that variable.

hope this helps someone.