Functions - 6


#1



Pactice Makes Perfect


It's returning 27 which is the cube of 3 however the second function isn't returning 1, I just don't see the solution and I'm sure its really easy one too. Hopeless noobie here.


Replace this line with your code. 
def cube(number): 
    number = 3 ** 3
    return number
    
def by_three(number):
    if number % 3 == 0:
        return cube(number)
    else:
        return False


#2

You're confused by this line I think..

if number % 3 == 0:

This line only tests and check if any number is divisible by 3 or not, If It is then it has to return cube of the number.

We have defined a function that does it (calculates cube of a number), so we simply call our function (Its our helper function, That helps our function by_three in calculating things.)
EDIT--

As mentioned by @datfatcat, You need to make some changes in cube function.
As it only calculates cube of 3, It should be a general expressions that calculates cube for any number.

Thank you @datfatcat Pointing that out, Overlooked the first function! :slight_smile:


#3

You overwrote the input, number here. So if user enters any number other than 3, it will return 27 regardless.

EDIT: I see your confusion.
Your first function should only cube whatever the input number is. Like if number is 1, then your cube function should return 1. If number is 2, your cube function should return 2**3 which is 8. This function is a "helper" and is a part of the main function, by_three.

As for your second function (by_three), it checks whether the same input, number is divisible by 3. If it is, it calls upon the cube helper function you wrote before. Or returns False otherwise.

For Codecademy's tests, they basically just call your by_three function to see if everything is correct as it also happens to include your cube function with it.


#4

Their def by_three(number) function looks correct according to the instructions.


#5

I'm still confused how to get the first function to call the second one, how it would be formatted. and make it return 1.


#6

Your calling is correct. Your second function calls the first one which is right. Your first one is merely a helper or a part.

It is just that one part in your first function that is off.

This part.
Just remove it and put return number**3.Because you are overwriting the input, number.


#7

Okay thanks, I think I should fiddle around with this before I move on to make sure I understand how it works. Thanks for the quick response.


#8

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