error message: Your function fails on censor(“hey hey hey”,“hey”). It returns “None” when it should return " *".

<What do you expect to happen instead?>
What i actually get when i print is the correct result, its just that the censored text is on the same line.


def censor(text,word):
whole = text.split()
for x in whole:
if x == word:
print ("*" * (len(word))),
print x,

<do not remove the three backticks above>


The operative word is, return. You have all the structure in place, and just need to construct a return value instead of printing.


Oh I see… Yeah I’ve realised that a lot of my errors in code is due to writing return instead of print.
Why is this so important though and how does this actually affect the code?


I replaced the print values with return but the code still does not proceed


Printing is a one-off with no imprint on the data structures. When we print something, the only record of it is on the screen. Functions are powerful objects in that they can compute or create a value from some other value and return it to the caller for further use, and proper recording of it.

How useful is this function in the given scenario:

def pi():
    from math import pi
    print pi

def circumference(radius):
    return 2 * pi() * radius

What will result is an exception since we cannot multiply None, which will be the return value of pi().

TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'

Now when we return pi, we get,

>>> def PI():
    from math import pi
    return pi

>>> def circumference(radius):
    return 2 * PI() * radius

>>> circumference(PI())


Please re-post your code so we can get to the bottom of it. Thanks.

Your code should start with an empty string or list, concatenate or append inside the loop and return the finished object.


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