FAQ: Learn Python - Functions - Practice Makes Perfect

3 posts were split to a new topic: Is % modulo different than / division?

A post was split to a new topic: Python - Function Practice - What’s wrong with this code? [solved]

12 posts were split to a new topic: [No Response] What is the solution?

5 posts were split to a new topic: Practice makes perfect

10 posts were split to a new topic: [Function Practice] Defining variables and the return function? [solved]

2 posts were split to a new topic: Practice makes perfect - cube

Another broken lesson, sigh

3 Likes

Broken how? Can i see your code and error message? Then i can make a bug report.

If you’re getting the “Failed to test your code” error, try refreshing the page. It fixed the problem for me after going through several frustrating lessons just dealing with it.

why its not printing the result?

Please see this topic:

How to ask good questions (and get good answers)

without seeing your code and other relevant information, its really difficult to tell what the problem might be. The most obvious cases being no print statement/function call or the print statement isn’t executed/reached for some reason.

Hi, i’m a beginner and i’m lost on this lesson. Any help/advice?
this is what i wrote:

def cube(number):
return number*3
def by_three(number)
if number % 3 == 0:
return cube(number)
else:
return print “enter different number”

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

There is some math in the lesson:

  • exponentiation
  • modulo

The logic includes,

  • functions
  • conditional

The function our main call will go to (by_three()) will conditionally call the helper function (cube()).

First the maths…

a => symbol for any number
a ** 2  =>  exponential expression  =>  a_squared
a ** 3  =>  a_cubed

We know that the square of a number is that number times itself…

a ** 2  =>  a * a

and the cube of a number is that number times itself, and times itself again.

a ** 3  =>  a * a * a

Generally, a raised to any exponent n is,

a ** n  =>  a * a * ... * a  (n times)

Module division yields a remainder from normal division instead of a quotient.

17 / 3  =>  5, with 2 remainder

so,

17 % 3  =>  2

Our helper function is very straight forward and it returns what the name says… cube of number

def cube(number):
    return number ** 3

The main function takes a number and examines if it is divisible by 3 (n % 3 == 0) whereupon it returns the cube of that number.

if number % 3:
    return cube(number)    #  call to the helper function

The return value from the main function will be the number returned from the helper function.

This line will return None to our caller (the call to the main function) since print() has no return. A better approach might be to simply return the message string, and print at the caller.

print (by_three(18))    #  5832
print (by_three(17))    #  enter different number
5 Likes

Why does " number % 3" set to equal 0?
For example: “if number % 3 == 0:”

In the expression we use == which is not an assignment. We are not setting, only comparing it to zero. The modulo operator is also called the remainder operator since it returns the remainder from division, not the quotient. Any number that is divisible by 3 will result in a zero remainder.

if n % 3 == 0  =>  then `n` is evenly divisible by 3.

Hi i used the print at the end of the code, but the values were not displayed.
Here is my code, can someone please explain me why this is not happening?

def cube(number) :
  return number ** 3

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

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