FAQ: Learn Python - Functions - Practice Makes Perfect

you nested the calls within the body of the function. Which would lead to recursion if you did call the function from outside the function/within the script

Hi guys,

Not sure if I’m being completely stupid but I’ve been stuck on this for a while now, and I can’t seem to find a solution in any of the provided resources.

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

def by_three(number):
if number % 3 == 0:
return cube(number)
else:
return “enter a valid number”
by_three()

by_three(3)

The code has changed a lot, I’ve tried using raw_inputs and moving everything around, but I’m completely at a loss.

Any help would be appreciated.

We cannot see your indentation. Hopefully the two call lines are not indented. If you wish to see the outcome, print() your call expression.

Thanks for your response, sorry I forgot indentation was important but I think I’ve got that correct (hopefully).

I added print by_three, which got the error shown in the photo.

I’m really not sure what to do here, just want to pass this lesson and move on.

************ Edit ***********

I didn’t realise there was a check solution button, but doing so highlighted the problem.

image

It wasn’t a bollean false (if that makes sense).

Thanks for your help regardless!

1 Like

Hi,
Regarding this exercise (Practice Makes Perfect), I can’t find out why print and return are different?

My code (apologies, for some reason my indents aren’t showing):

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

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

print cube(4)
print by_three(3)
print by_three(4)

This all prints:
64
27
False

So I understand this code! (Phew)
But the hint for this exercise says:
“Make sure both functions return their values rather than print ing them.”

Why, within the functions themselves, can I not replace return with print?

For example:

def cube(number):
print number * number * number

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

cube(4)
by_three(3)
by_three(4)

This prints:
64
27
None
False

So my question!:
Why is print, instead of return causing a problem here? Why has “None” appeared? What is the difference between print and return in this context?

Thank you for your help!
Kind regards,
Alex
(2nd time through the Codecademy Python 2 course by the way and enjoying it a lot more this time! Assuming I’m not alone in nothing making any sense first time round!?)

print and return do different things. print just prints some output. return hands back data to the caller.

if a function doesn’t return anything, you get None (which means absence of a return value)

Thank you for your reply!
That’s some good clarification. What I still don’t understand though is, why does print not work within the function in this code? Why must I choose return instead? and refrain from using print until I call the function’s argument?

To my (clearly misunderstood) logic, either way of writing the code should create the same result in the terminal…

Thanks!

exercise validation. Normally this would depend on program requirements, but return would have my preference, given then we can use the result for other things.

Nice. OK thank you for this clarification! Good to know why return is often preferred. Thank you!

Their solution…

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

My solution…

def cube(number):
  return number ** 3

…which was not considered correct. Are they not the same? .

I did not post the full code as the rest was identical.

1 Like

Hi guys, this is my code but I keep getting the error message “Did you define a function called by_three?” Really don’t know what I am doing wrong??

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

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

If we attempt to call the function:

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

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

print by_three(15) # print() for python3

we get an error. Which means the function can not be accessed.

which means it either: doesn’t exists, or you have a scope problem.

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


def by_three(number):
  if number/3 == int:
    return cube(number)
  else:
    return False

That’s what I did, should have worked. It be like that man, but well. What can you do?

your code doesn’t work, easy enough to test:

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


def by_three(number):
  if number/3 == int:
    return cube(number)
  else:
    return False
    
print(by_three(3))

I should have gotten 9, but instead I get false

There are several problems, an integer (for example: 1) does not equal its data type (int). Then you would need to check if the result of the division is of type integer

but this also doesn’t work, given python2 will always give an integer (floor/round down) and python3 gives a float (3 / 3 = 1.0)

I noticed in Functions Calling Functions cubing 2 with the following operators 2**2 results in 4 in the workspace. While 2 * 2 * 2 will return the correct answer of 8. Cubing other numbers with ** is only 2 is affected.

Can anyone explain and fix this?

‘Cubing’ means raising a number to the third power.

n ^ 3

In Python we use the exponent operator to raise to a power:

2 * 2 * 2  =>  2 ** 3