Issues with censor


My code returns the error message "Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** " when it should return "*** *** ***"."
I don't understand why the space at the end of the last set of asterisks matters or how to remove it.

Here's my code:

def censor(text, word):
    text = text.split()
    value = ""
    for word in text:
        A = "*" * len(word)
        value = value + A + " "
    return value

Please help!


you learned string slicing, you could use it here to remove the last character


I did noticed this.

Even you remove last character from this, It creates a new error!

I hope Im getting what you mentioned ,otherwise Pardon me! :slight_smile:


i see what you mean, this function just censor every word in the sentence.

@tabethar, you should only censor if the word in the sentence equals word (your function parameter)


Exactly!! :thumbsup:


i sometimes work in one step at a time, giving the learner change to deal with any new arising error message, they can ask if they need further assistance

i would like to think, this increase they skill/understanding


:thumbsup:‬‬‬ ‬‬‬‬‬‬‫


You can do this exercise without converting the string to a list and without a for loop. Try using only the replace method and reassigning the replacement to text.

I didn't realize this at first. I started out splitting the string out into a list and then trying to loop over the list to replace instances of word. I kept that version because I would still like to learn to make it work since it might be useful to know how to make a list from a string of words, chars, etc, then edit by index as needed.

But to pass this all you need is string.replace()


actually, i disagree. using replace doesn't deepen your understanding and doesn't improve your problem solving skills

and replace() isn't perfect, what if you have:

censor("this is not good", "is")

then this gets partly censored while it shouldn't

learning is more important at this stage then using a built in function


I agree with you there, but it's handy to know when it will and will not work. The context and problem matters.

That's why I used replace to solve the exercise, but I plan to revisit it and attempt to solve using other means.


At least we can rest easy knowing that the replace() method will work for ("imagine the clusterfock", "fock") lol


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