FAQ: Learn Python - Functions - Practice Makes Perfect

Community%20FAQs%20on%20Codecademy%20Python%20Exercises

This community-built FAQ covers the “Practice Makes Perfect” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise Practice Makes Perfect:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: Does return do the same function as print does?

A post was split to a new topic: Modulo operator

2 posts were split to a new topic: “Check the console window for errors!” Error

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

4 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)