Censor - is there a way to solve it with str and not convert it to list?


#1



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


I'm getting an error that says "Your function fails on censor("hey hey hey","hey"). It returns "he* he* he*" when it should return "*** *** ***""


I've tried several different things, none of them worked, and when I came to this forum to see if anyone else has struggled with this, I noticed that many of the solutions require converting var word and var text to a list first. I was wondering: is there a way to solve this challenge with strings, or is there no way to censor words without converting them to lists first?

My code (that's generating the error):


def censor(text, word):
    censored_text = ""
    censored_word = ""
    for i in word:
        censored_word = word.replace(i, "*")
    if word in text:
        censored_text = text.replace(word, censored_word)
    return censored_text


#2

str.replace which you are using in your code does everything that your function needs to do, so if you use that method, then what is the rest of the code doing?

What the exercise wants you to do is to think about what actions need to be performed to get the desired result and then implement whatever you thought up. There may be ways that the language makes you do things in certain ways, sometimes trivialising tasks, but stick with your own concept and implement exactly that.


#3

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