Censor


#1



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


I'm not sure exactly why my code isn't working, I have a feeling that it is something simple. but i keep getting this error message: "Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "None" when it should return "*** *** ***"."


This is my code..

def censor(text,word):
    z=len(text)
    z='*'*z
    t=text.split(" ")
    w=str(word)
    k=[]
    for i in t:
        if i in w:
            i=i.replace(i,z)
        else:
            k=' '.join(k)
print censor('how are you', 'are')


#2

There is no return statement in your function, hence the error.

That said, your function won't work:
1) Are you sure you want z to be equal to the length of the whole string?

2) After i=i.replace(i,z) what should you do with the filtered word? At the moment it i is going to be overwritten in the next iteration. Maybe place it into a list? Also, is
i=i.replace(i,z) not just the same a z?

3) Also, the else statement, this will be for words that shouldn't be censored. What should be do with them? At the moment, you are turning k into a string that will be made up of the items already in the list that k points to.

4) No return statement, as above.

If you need more ifno, just ask.


#3

Thank you for your help, I think I'm almost there. I have changed my code slightly (I think in the way you told me to), however i'm still getting an error. This is my new code:

def censor(text,word):
    z=len(word)
    z='*'*z
    t=text.split(" ")
    w=str(word)
    k=[]
    for i in t:
        if i in w:
            i=i.replace(i,z)
            k.append(i)
            return k
        else:
            k=' '.join(k)
            return k
print censor('how are you', 'are')

The error i'm getting is, "Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "['**']'' when it should return "*** *** ***". "
(Within the square bracket there are 3x* but it caused a formatting error when entering it.)


#4

here:

if i in w:

you can use in, but that is pretty extensive. Why not simple check if i equals w?

here:

            i=i.replace(i,z)
            k.append(i)

why do a replace action first? You can append z directly to k, can you explain me why replace is relevant?

when i (current word from text string) doesn't need censoring (else), we will need to append i to k

this way, we create a list which will create a list with our censored sentence

then after the loop, we need join the list into a string and return it

Currently, why would you use .join() everytime i doesn't need censoring?

Finally, its important to understand, a function ends the moment a return keyword is reached. So, currently your loop ends in the first iteration of the loop

you only want to return after the loop (when the censoring is complete)


#5

Thank you, that really helped, I've done it now


#6

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