Censor


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4

Can anyone tell me is wrong with this bit of code ?


def censor(text, word):
    
    text.split()
    text = []
    for letter in range(len(text)):
        if letter == word:
            new_text = "*".join(text)
            print new_text
            return new_text


#2

You have wiped out the text list by redefining it as an empty list.

words = []

When using range(), letter will be an integer.

text[letter] == word

Writing a return inside a loop exits the loop after first iteration.

text.append()

is the tool to use in the if statement. Then outside of the loop,

return " ".join(text)

#3

I want your brain mate ... thank you for explanation.


#4

This isn't perfect yet but hopefully getting closer:

What do you think ?

def censor(text, word):

text.split()
word = []
for letter in range(len(text)):
    if text[letter] == word:
        "*".append(word)
print " ".join(word)
return " ".join(word)

#5

Again, you are wiping out the censor parameter. It should not have the same name as any other variable.

words = []

result = []

Anything that is unique.

    if text[letter] == word:
        result.append("*" * len(word))    # if word is "hey" this will be "***"
    else:
        result.append(text[letter])

We are not asked to print anything in the function, just return the censored text.

Aside

Semanitcally speaking, the iterator variable, letter is not very accurate, and is actually somewhat misleading. This is not a letter, as such, but a word, or term

for term in text:
    if term != word:
        words.append(term)

or,

for term in range(len(text)):
    if text[term] != word:            
        words.append(text[term])

#6

I've got headache :smiley:


#7

This is what I've got and please don't pull your hair off (lol)

def censor(text, word):

text.split()
new_text = []
for term in range(len(text)):
    if text[term] != word:
        new_text.append("*" * len(word))


return " ".join(text)

I'm getting this error ... Your function fails on censor("hey hey hey","hey"). It returns "h e y h e y h e y" when it should return "*** *** ***".
Apparently all I achieved here is putting spaces between letters.


#8

Updated code:

def censor(text, word):

text.split()
new_text = []
for term in range(len(text)):
    if text[term] != word:
        new_text.append("*" * len(word))


return " ".join(new_text)

censor("this is inappropriate content", "inappropriate")

This code's giving me "Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** *** *** *** *** *** *** *** " when it should return " *** ***"."


#9


#10

I've been missing a variable ... and 'else' statement as well .... :-/


#11

yeah man, I feel you, I´m a beginner to programming and sometimes I just find myself searching for mistake for hours :smiley: and as soon as I find out that I just did something stupid, i.e. wrong indent for return I´m just laughing at myself :smiley: hang in there bruh :wink:


#12

Yeah dude ... this exercise really making me jump under the train :smiley:


#13

My dignity has been crushed .....


#14

So far, the worst exercise for me was reverse ... I did it with the help of this forum, but still... I don't get it completely :smiley: gotta ask my dear friend programmer :smiley:


#15

Gotcha mate .... I've got headache almost on every exercise since these challenges started. And still don't get some solutions no matter how many times I read it through. Forum is great and advisors on hand are also very useful .... I just wish my brain would take this all in easier ...


#16