Python Functions: Practice Makes Perfect ~ Glitch or myError?


I’m having an issue with my code, and not sure whether it’s a glitch in the system or my own error. My code works in the console when I issue a print command… but I’m getting the following error message.

‘by_three(1) returned ‘False’ instead of False’

Here are the instructions.

1. First, def a function called cube that takes an argument called number. Don’t forget the parentheses and the colon!
Make that function return the cube of that number (i.e. that number multiplied by itself and multiplied by itself once again).
Define a second function called by_three that takes an argument called number.
if that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False.
Don’t forget that if and else statements need a : at the end of that line!

And, here is my code:

def cube(number):
    return number ** 3

def by_three(number):
    if number / 3:
        return cube(number)
        return "False"
print by_three(21)

I just added the last print command in this last round of testing to see whether it simply needed a defined number to work. With the print command, I do get the correct answer in the console box, however, I still get the error message.

I do not understand the error message, but I have tried it both with single quotes and double quotes. I’m stumped! Please help!



Hi @violinchic92,

You only tested one case, wherein the function argument was 21, and got a correct result. However, there are other cases where your function would produce an incorrect result.

Try this, for example …

print by_three(4)

Though 4 is not divisible by 3, your result would be 64.

The problem is that this if header does not test correctly whether number is divisible by 3

    if number / 3:

Instead, use the modulus operator, as follows …

    if number % 3 == 0:

There is another problem. Where the instructions ask that by_three return False, you have …

        return "False"

Note that the instructions do not have quotes around False, and that is because you are being asked to return a boolean False rather than a string. To remedy the problem, remove the quotes.


Oh, I see! Thank you so much!