Censor, if not working


Write a function called censor that takes two strings, text and word, as input. It should return the text with the word you chose replaced with asterisks.

For example:

censor("this hack is wack hack", "hack")

should return

"this **** is wack ****"

Assume your input strings won't contain punctuation or upper case letters.
The number of asterisks you put should correspond to the number of letters in the censored word.

When len(text) and text.index(s)+1 are the same (which happens, check), it should not add a space to the next word, instead, what happens is that it adds it either way.

def censor(text, word):
    text = text.split()
    new_text = ""
    for e in text:
        if e == word:
            w = text[text.index(e)] = ('*' * len(e))
    for s in text:
        print text
        print int(text.index(s)+1)
        print int(len(text))
        if len(text) == text.index(s)+1:
            new_text += s
            new_text += s + " "
    text = "".join(text)
    return new_text
    print new_text
print censor("Steven and Javo", "Steven")


Do you have a question about anything in your code?


Yeah, the question is why it is not working like it should, that could (i'm pretty sure) is spot on.


What should it do and what does it do instead?

"work" doesn't carry that information!

What has you stuck? What information are you missing to solve it?


Basically what the program should do, is censor the second argument in the function. The first argument is a sentece, eg: Salt and pepper, and then the second, pepper. What it should return is Salt and ******. The code seems to be alright, but when executed it adds an extra space (Salt and ****** ).


Can't be that many places where spaces are added, can there? I think you're able to identify where it's occurring.

(Still waiting for a question about the part you're stuck with)


for s in text:
print text
print int(text.index(s)+1)
print int(len(text))
if len(text) == text.index(s)+1:
new_text += s
new_text += s + " "
Basically here


So what is it doing and what does it need to be doing differently?


When it comes to the ==, it does not really work. Can you run it?


There we are again with "work" instead of describing what should happen and what happens instead! The difference therein says a lot about what you need to change. Comparing what action you want it to be doing to what the code is saying is how to figure out what you need to adjust.

That word. Banned. Okay? No more!

If you don't know what it is doing in the code, then you can add print statements that show what the expression is using. Then you can consider if that's the right information, if not, then that's what to address next. Or if it's right, then the information isn't treated correctly.

You have already added those print statements. Which is good. But when you're asking about this you're .. not presenting your findings or asking what to do with them or asking how some specific operation is working that you rely on there. Cut to the part you're stuck on, right?


Basically what it sounds like. len(text) will the = 3 (changes depending on the phrase), and text.index(s) win go up for each for loop, until they will be the same number, once that happens (it is supposed) to add s to new_word without adding a space, but when it comes to doing it, it just skips to the else (where it does add the space), meaning the == did not go through and did not detect the same number.


Yeah but I'm not reading any of that until I know why you're stuck. I can wave my wand and fix it for you but.. not really to any use for either of us. And if you're not getting it to do what you want chances are you've misunderstood something there and I would interpret it differently from you.

And now I have some idea of what your intention is with that code, which I can compare with the code.

What do you think 'aloha'.index('a') returns? What would your condition be "doing" when it's at the end of that?


I see now that there was a problem description!

That's.. exactly what i was looking for the whole time. I completely failed to read it as it appeared to be part of copy-pasted instructions. :confused:


Guys I used replace() function instead. Here is the code:


hiii.. would you please explain what split() does?


You're using it well ... almost reduced to a 4 line code.. great going


Suppose your string is a= "Hello I am learning python" now split() method will split the string into words and return a list of the words in the list.

the default delimiter of the split() method is SPACE. see the following code

In the 2nd print statement we gave an argument to split() method as "e". So the string will be spliced upto "e" and stores as an item in the list.

Sorry for clumsy explanation


Got it now.. Thanks a lot



I just used split() and len() methods. Check the following code;

def censor(text,word):
arr = text.split()
new_text = ""
for x in range(len(arr)):
if (x > 0 and x < len(arr)):
new_text += " "

    if(arr[x] == word):
        new_text +=  len(arr[x]) * "*"
        new_text += arr[x]
return new_text


This worked perfectly for me. Thank you.