Code returning 1 instead of 8 and vice versa


#1



https://www.codecademy.com/courses/python-beginner-c7VZg/1/5?curriculum_id=4f89dab3d788890003000096#


When I run the code it says that cube(1) returns 1 instead of 8 and vice versa with cube(2) also.
can anybody tell me what that means???


def cube(number):
    if number % 3 == 0:
        return 8
        print "n is dibisible by 3"
    else:
        return 1
        print "n is not"
    
def by_three(number):
    if by_three % 3 == 0:
        return 1
        print "n is not"
    else:
        return 8


#2

Hi if you followed the instruction

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).
so your code will look like that

def cube(number):
    return number ** 3

or 

def cube(number):
    return number * number * number

Hope that could help you


#3

Now it is saying that by_three returned False instead of 27.

Here is my new code.

def cube(number):
return number ** 3
print "number is dibisible by 3"

def by_three(number):
if number % 3 :
return False

    else:
        return False

can you please tell me what is wrong with it?


#4

then the intrusction said
if that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False.
and you made it

def by_three(number):
    if number % 3 :
        return False
    else:
        return False

but that line is missing something

 if number % 3:

you should check if number is divisible by 3.
Do see now what is missing?


#5

I just would like to point out that problem is not in the condition of if. Condition is correctly checking if the number is divisible by three.

Problem is in the returned value if number is divisible by three (else block). There should be return cube(number) instead of return False.


#6

HI the problem is the if statement because the

if number % 3:

should check if number is divisible by 3 so it should be like that

if number % 3 == 0:

the remains of the division should be equal to 0
and the else statement
just look at the instruction

if that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise , by_three should return False.


#7

I am just asking - what is the difference between:

if that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise , by_three should return False.

and

if that number is not divisible by 3, by_three should return False. Otherwise , by_three should call cube(number) and return its result.

:slight_smile:


I think that we should help users by proposing minimal changes. Feel free to disagree, I just wanted to toss in my solution :slight_smile: