Error on codeacadamy lesson on functions Practice Makes Perfect: This is my code: def by_three(n): if n % 3 == 0: cube(n) return n else: return False def cube(x): return x ** 3 by_three(11) by_three(12) by_three(13)
I am guessing, but I think the function either has to return a value or a boolean, so if you use something besides False it might work.
Put a =by_three(11) b =by_three(12) c =by_three(13) print a print b print c I get False, 12, False
If the purpose of the function is to simply show the values, replace 'return' with 'print' instead. The return function is used when you want the value returned to be reused in another function.
def by_three(n): if n % 3 == 0: return cube(n) else: return False def cube(n): return n ** 3 by_three(11) by_three(12) by_three(13) If you want to show the result, replace return with print
You are not returning the result of the cube function in your code, you are simply returning the value of the parameter when the parameter is even. The code from DanSraer is correct.
@DanSraer has already done it. I just want to mention why the error might come... when you are defining the cube function, you are using cube(x), where x points to the parameter,. This function is used in the by_three(n) function but with 'n' as the parameter call ... Using either 'n' or 'x' in both the functions should do the work.
Join our real-time social learning platform and learn together with your friends!