Censor help please?


Practice Makes Perfect: Censor

Somehow my code seems to do absolutely nothing.
I get the response: Your function fails on censor("hey hey hey","hey"). It returns "hey hey hey" when it should return "*** *** ***".
So basically the if statement is not recognizing the criteria like I was hoping for?

def censor(text, word):
    text_list = text.split()
    new = ""
    for x in text_list:
        if x == word:
            x = "*"*len(word)
            x = x
    new = " ".join(text_list)
    return new


No change is taking place because a change to x is lost in the transience.

In other words, x is discarded after viewing.

We can do something with x, that would have an effect; but, to x, has no effect that we can register.


So I changed x to word if I understand your suggestion correctly.

def censor(text, word):
    text_list = text.split()
    new = ""
    for x in text_list:
        if x == word:
            word = "*"*len(word)
            word = word
    new = " ".join(text_list)
    return new

But I still get the same error message.


how is this better? How will this lead to your desired result (a censored string)?


It didn't =(
I was following my best understanding of mtf's suggestion, but it did not change the result.


let me rephrase the question then, how do you think the changes you made (based on mtf suggestion )will improve your program?


It sounded like my mistake was that x is only used to define passing through the iteration, and changing x doesn't actually change the list itself. So I switched the instances of x in which I was trying to change the list itself with word.


but if you want to manipulate a list, you need indexes. look:

a = ["hello", "world"]
a[1] = "thestartcloud"
print a

see? This will actually update the list

how could we loop over a list and get the indexes? You learned this


We are going down the garden path, here, and this needs to stop;

All that has really happened is the censor input variable is overwritten.. There is a profound ignorance showing here that needs to be addressed.

Put your pride aside and ASK for help. Just showing us code that is clearly misunderstood is not going to help you. Until you are able to trial and error on your own, now is not the time.

Go back to the beginning of the track, and bring yourself back to this point.


Lets break your code down and see what it is doing:

censor("test test test", "test")# So we've called the function with test as the censor word and "test, test, test" as the text string.

def censor(text, word): # text = "test test test" and word = "test"
    text_list = text.split()  # text_list is ["test, "test", "test"]
    new = "" # new is now an empty string
    for x in text_list: # item in text_list (["test","test","test"] assign the item to x (x = "test")
        if x == word: # if the word x is the same as the censor word then continue
            word = "*"*len(word) # The censor word now equals the length of the censor word but as ***
        else: # the above if statement was false
            word = word # the censor word stays as being the censor word
    new = " ".join(text_list) # new now equals each item in text_list with a space put bewteen them, in one string. "test, test, test.  Nothing has been done to text_list at this point.
    return new # return new, which is "test test test"

Look at what your code is doing, you should notice a few things you didn't intend. Fix them and then see if you have more issues you need help with.


I don't really have much pride here, I'm pretty new to programming and am just trying to learn through codecademy. I am trying to ask for help, and just posted my code because I don't know what's wrong with it (as the prompt suggested when starting the discussion).


Then go back to the start. We can answer all your questions but not be doing you any favors.


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