10.Censor


#1

Hi folks,

Once again, I have a solution that is showing up as a correct answer, but not allowing me to pass on to the next lesson. (How do we post pictures??) Here's what I came up with (no help!):

def censor(text, word):
        print text.replace(word, '*' * len(word))

censor("You cant touch this touch that touch those", "touch")

It returned the following:

You cant ***** this ***** that ***** those
None

Any ideas? I think the "None" is what is preventing this from passing.

Thank you!


#2

You need to do

return text.replace(word, '*' * len(word))

instead of

print text.replace(word, '*' * len(word))

#3

Woohoo!! It works! So, do all functions need to include a return?


#4

No not necesarily
The print() statement(call) "prints", a statement in the console.
The return statement terminates your function(method) and hands back a value to its caller.
So in this exercise we want the program to give us a text that is censored(so that we could store it in a variable).
we can also do this

censored_word = censor("You cant touch this touch that touch those", "touch")

#then now we can print it like this
print censored_word

if you print it then nothing is handed back to you the caller but rather the result is printed(sent) to console


#5

Hmm, I think I understand this, but how do I know when I want something returned to me versus just printed to the console?


#6

I have the same problem, when I run my script, the editor returns the following message:

¡Uy! Probá otra vez. Tu función falla en censor("hey hey hey","hey").
Devuelve "*** *** *** " cuando debería devolver "*** *** *** "

obviously my answer is right


#7

Can you include your full code?


#9

Mostly because the exercise will explicitly tell you to do this :smile: In this case exercise 10 said:

Write a function called censor that takes two strings, text and word, as input. It should return the text with the word you chose replaced with asterisks.


#10

Thanks for replying, but I meant in real life. I haven't worked on any python projects yet, but I doubt a client will tell me which one I should use...


#11

Hahah, fair enough! :smiley:

Usually in real life you won't be making such small programs such as these. Instead, you will be stringing many different functions together to reach an ultimate goal. In that regard, the only thing you can say is "it depends".

In many cases functions will perform tasks which will require little to no feedback to the originating process. In others, you'll definitely want feedback or resulting values. Printing on the other hand is only meant for visible output (be it to the user or to output files)...

Here's a real world example in meta-code:

def generate_uid()
    # get next available uid
    # check that new uid is within bounds
    if inbounds:
        return uid
    else:
        return -1

def generate_gid(username)
    # get next available gid
    # check that new gid is within bounds
    if inbounds:
        #create new group with gid and name = username
        return gid
    else
        return -1

gcos = raw_input("Whose account are we making? ")
username = raw_input("What's the desired username? ")
uid = generate_uid()
gid = generate_gid(username)

useradd = "useradd -u %s -g %s -d /home/%s %s" %(uid, gid, username, username)
print "Made user account %s, with uid and gid of %s, %s.) %(username, uid, gid)

It's not great, but it's a start.


#12

Aha! Makes sense. Thanks!


#13

WTF theres a replace function? I have been struggling with this.
def censor(text, word):
for letter in word:
append.cword("*")
for letter in text:
newword=""
while letter is not " ":
append.newword(letter)
if len(newword)= len(word):
x=0
for letter2 in newword:
if newword(letter2) = word(letter2):
x=x+1
if x=len(word):
newtext=newtext + cword
else:
newtext=newtext + newword
else:
newtext= newtext + newword
return newtext

a replace function seems easier though..........but why isnt mine working


#14

umpf! my indentations dont show, how so?


#15

You need to use the codeblock button, the one with </>, to mark the text as code :smile: See also the friendly HOWTO on the Codecademy forums.


#16
def censor(text, word):
for letter in word:
    append.cword("*")
for letter in text:
    newword="" 
    while letter is not " ":
        append.newword(letter)
    if len(newword)= len(word):
        x=0
        for letter2 in newword:
            if newword(letter2) = word(letter2):
                x=x+1
        if x=len(word):
            newtext=newtext + cword
        else:
            newtext=newtext + newword
    else:
        newtext= newtext + newword
return newtext

#17

I have to say that your matching criteria are very interesting! :smiley:

You're reading the string char-by-char. As long as the char is not a whitespace, you add the chars to a newword. Then you check to see whether the newword is just as long as the to-be-censored word. if it's not, then you just add the word to newtext, otherwise you're doing another char-by-char check it seems...

I'm sorry, this is all over the place, not in the least because it'll stop working upon encountering the first space (if I'm reading this right). The good thing is that the Hints section points you towards the right solution: string.split(). This will split the string into a list, one word per list-item, which will allow you to easily dike-out the unwanted words.


#18

Thanks a lot. Yeah, my suggestion was a work in progress. A string split was just what I needed, although it must be possible to make one. But thank you.


#19

Yeah, what about that! Not knowing the replace function... here is how I did it.

def censor(text,word):

text2 = text.split()
rplc = "*"*len(word)

for x in range(0,len(text2)):
    if text2[x] == word:
        text2[x] = rplc
        textfin = " ".join(text2)
return textfin

print "censored text:",censor("marco polo was a polo player in the pool","polo")

returns this: censored text: marco **** was a **** player in the pool