Practice Makes Perfect


#1

https://www.codecademy.com/courses/learn-python/lessons/functions/exercises/practice-makes-perfect

I have created this code(see below). The editor says its oke, but the consoles show no error or print output. What did i do wrong?



def cube(number):
  return number**3
def by_three(number):
  if number % 3 == 0:
    print "Number is divible by 3"
  else:
    print "N is not"



#2

from the instructions:

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

by_three should call cube(number) and return its result != printing a string (you print a string)


#3

Tnx for the verry fast reply Stetim. Code above was for testing purposes.

Real code is accepted, but no result in the console:

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

#4

functions only execute when called, you never call the by_three function, so it never executes


#5

I need to call it with a value right? Like

by_three(27)


#6

given your function has a parameter (number) you indeed to need supply an argument value at function call to satisfy the function parameter


#7

Sorry for bothering and already tnx for your great help. Its not giving any output yet.
Its like this right?

  def by_three(27):
  if 27 % 3 == 0:
     return cube(?)
  else:
     return False
  bythree(27)

#8

What did you do? Your code was okay, now its not really okay

where did your function parameter number go? The argument/value supplied at function call gets passed/copied into the function parameter

the function call executes the function, so it should be place outside/after the function. (indent determines nesting in python)


#9

it was for understanding.

My code now:

def cube(number):
  return number**3

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

by_three(27)
  

2 questions: I have called the function by-three and give it a parameter. But does nog return the value of cube(number).
I have not called cube(number) and gives it a parameter?


#10

to print what a function returns we need to do:

print by_three(27)

cube() is a helper function in this case, we call it when number is divisible by three:

  # when number is divisible by three
  if number % 3 == 0:
     # call cube and return the result
     return cube(number)

#11

Great explanation Stetim. Thank for your time :smiley:


#12

You’re welcome :slight_smile:


#13