Censor_ship


#1

Hi guys!
Why isn't this working?

def censor(text, word):
text = text.split()
censor = ""
for i in text:
if i == word:
i=""len(word)
censor = " " + i

return censor(text, word)

#2

Lots of issues in the above.

Let's start with the last line so we get it out of the picture. This is not a recursive function so we are not retuning a call to the same function we are returning from. The function call clearly belongs higher up the scope chain.

censor("This is some text", 'text')

If the function is supposed to print, there won't even need to be a return value, just a couple print statements.

    print "*" * len(word),
else:
    print i,

#3

Thanks! so we just omit the last line, but still it isnt working

```

`def censor(text, word):
text = text.split()
censor = ""
for i in text:
if i == word:
i=""len(word)
censor = " " + i

it returns NONE.


#4

No, we didn't jsut omit the last line. We moved it to another scope. The remaining code is still incorrect. The lines above are meant to replace everything in your if statement.

This line may be removed. It is destructive, and does nothing meaningful.


#5

thanks for trying to help but i'm afraid completely miss your point. by the way, asteriscs in my code went missing somehow - so it's not that stupid as it possibly seems.


#6

def censor(text, word):
text = text.split(" ")
censor_text = ""
for i in text:
if i == word:
i=""len(word)
censor_text += " " + i
else:
censor_text += " " + i

return censor_text

and it says now
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns " *** *** " when it should return " *** ***".

How can i add the "." after the inverted commas?
and another question: i've been trying to avoid ELSE statement as i thought it is extra, isn't it?Preformatted text


#7

Okay, so first we need to learn how to format code samples in posts...

That will make it possible to see all the characters as well as any indentation, making it readable and discernable. Another thing I would recommend avoiding is using the function name as a variable inside the function (which I now see below you have done). This too will help maintain readability.

def censor(text, word):
    text = text.split(" ")
    censor_text = ""
    for i in text:
        if i == word:
            i=""len(word)
            censor_text += " " + i
        else:
            censor_text += " " + i

    return censor_text

This is your code from above. Take a look at my earlier example and see how it can be applied here.

i=""len(word)

We cannot modify the iterator variable inside the loop. i is the iterator and should be treated as read-only.

            censor_text += "*" * len(word)
        else:
            censor_text += i

The only difficulty we are going to have with the string is the leading or trailing space character if we add spaces to each value added to the string. The SCT will see it and not expect it. So if we write,

            censor_text += "*" * len(word) + " "
        else:
            censor_text += i + " "

Then the return value needs to be trimmed of white space.

return censor_text.strip()

#8

Thanks a lot!
"We cannot modify the iterator variable inside the loop. i is the iterator and should be treated as read-only." - this is what i missed!


#9

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