Censor exercise help


#1



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


I tried this exercise using two functions and both of them did not work and I would like to find out the reason why.

the first function below doesn't work at all.
second function only returns "hey *** ***", leaving the first hey uncensored.

I will be very grateful for any explanations on why the functions fail to work! thanks in advance.


###function 1
def censor(text, word):
    til = text.split()
    for w in til:
        if w == word:
            w = "*" * len(word)
    return " ".join(til)

###function 2    
def censors(text, word):
    til = text.split()
    for w in til:
        if w == word:
            til.remove(w)
            til.append("*" * len(word))
            print til ##just to see the list
    return " ".join(til)


#2

###function 1
def censor(text, word):
    til = text.split()
    for w in til:
        if w == word:
            w = "*" * len(word)
    return " ".join(til)

This function produces censored words (if you place print w in your if statement, you'll see) but you're not doing anything with them. I would recommend storing the censored word (w) in a list, then using that list in the join statement at the end.

###function 2    
def censors(text, word):
    til = text.split()
    for w in til:
        if w == word:
            til.remove(w)
            til.append("*" * len(word))
            print til ##just to see the list
    return " ".join(til)

The reason this doesn't work is that you're modifying the very list (til) that you're currently looping through. If you instead use a different list to append the values to (and don't remove the values from til), it should work.


#3

thanks for the reply! there's something I don't understand regarding the first function though.

from the way I interpret it:
for e.g., in the string "test test test", it will produce the list ["test", "test", "test"]. then the function will loop through the list and for every "test" it sees ("test is the censored word), it will replace the "test" with "****" since I'm looping through the list.

however, i followed your advice and added the "print w" and realised I just got three of the strings "****" and the list remains unedited. why is that the case? in the loop, w was the original word in the list I wish to censor, shouldn't setting w to be asterisk directly modify the list?

*EDIT:
it's ok! I found the answer at this website: http://stackoverflow.com/questions/19290762/cant-modify-list-elements-in-a-loop-python. apparently, when I did it that way, I just modified copies of the elements within the list, therefore not modifying the original list. thanks so much still!


#4

This is my solution for this problem. I just used "replace" method
def censor(text, word):
return text.replace(word, ("*" * len(word)))


#5

Haha you make my day man !!! Now few hours im on this exercice and you solve it in two lines :confused: .
Thanks anyway


#6

that's cleaver. But I think the idea of the exercise is to create your own replace function :slight_smile:


#7

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