6 Practice Makes Perfect - Why use return in by_three( ) function?


#1

Here's my final code which I got correct.

def cube(number):
return number ** 3

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

The first time I coded it however, I did the following:

def cube(number):
return number ** 3

def by_three(number):
if number % 3 == 0:
** cube(number)**
else:
return False

I got the error that it returned "None". My confusion is this: If def_cube already returns number ** 3, why do I have to again use return in the if statement inside by_three?


#2

If by_three calls cube, and cube returns, then control of the program is back in by_three

If by_three is to return anything, then there needs to be a return statement in by_three

return only exits the current function, it doesn't cause all functions currently on the stack to exit


#3

I was thinking it was something like this but your explanation makes it very clear, thanks.


#4

I did exactly like you, but it gives me error on line 4.


#5

def cube(n):
return n**3

def by_three(n):
if n % 3 == 0:
return cube(n)
else:
return False


#6

I think the best answer for the exercise is

def cube(number):
return number**3

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


#7

im sorry, but why you write ==0?


#8

I was wondering the same thing.....does this code look ok

def cube(number):
return number**3

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


#9

can anyone please show me the right one so I can compare it with mine to c what is wrong


#10

it still dsnt work......wht to do????


#11

it says
File "python", line 2
return number ** 3
^
IndentationError: expected an indented block


#12

Hi everyone,

I have the below formula, however, I have a question why does it need the semi colon after return cube(number);
I want to understand the idea behind this, why does a colon not work here for example?.
thx

def cube(number):
"""returns a cube value to the console"""
return number **3

def by_three(number):
"""checks if the number is divisible by 3 and returns the value"""
if number % 3 == 0:
return cube(number);
else:
return False


#13

it dsnt work aarrgghh


#14

hi

def cube(number):

return number**3

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

its correct