10. Censor - when I print it its correct, but when I return it its wrong


#1



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


Hello helpers. I'm having trouble with my code. When i print the code, the console shows that it works as expected, but when i replace print with return it get an error:

Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "hey" when it should return "*** *** ***".
How come the outcome for the print and return differs?


def censor(word,text):
    l = []
    x = len(word) * "*"
    l = text.split()
    print l
    
    for n, i in enumerate(l):
        if i == word:
            l[n] = x
        k = ' '.join(l)
    print k
    return k
censor("hey", "hey hey hey woop")


#2

What do you mean? what is the output when you for print and return?


#3

['hey', 'hey', 'hey', 'woop']
*** *** *** woop

the above is the output.
the second line is print k
but when i use return k, i get the error:
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "hey" when it should return "*** *** ***".


#4

Hi, I think the error checking algorithm is passing its arguments in the order (text, word) and expecting the result based on this. Try switching the order of the arguments in your function definition and see if this makes a difference.

Also, in order to 'see' what your function is actually returning, try:

print censor("hey", "hey hey hey woop")

You'll see that your print and return don't differ at all. What differ are the two variables l and k, both of which you print inside your function.

Hope this helps!


#5

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