Issues with censor


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#


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!


#2

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


#3

I did noticed this.

Even you remove last character from this, It creates a new error!
value[0:len(value)-1]

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


#4

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)


#5

Exactly!! :thumbsup:


#6

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


#7

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


#8

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()


#9

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


#10

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.


#11

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


#12

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