FAQ: Learn Python - Functions - Practice Makes Perfect

faq

#1

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

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


#3

A post was split to a new topic: Modulo operator


#4

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


#5

In the solution, how does:

if number % 3 == 0:

Ensure that the number is divisible by 3, and not just ensure that the number is 3? Surely if I call:

by_three(9)

The function should return False, because 9 / 3 = 3, not 0. Yet when I tried this i got 729 as an output.

There’s clearly something I’m missing surrounding the == 0 part of the code but I’m not sure what?


#6

this condition uses the modulo operator (%), which gives the remainder, which is different from division operator (/)


#7

Of course! I’m a tired idiot, thanks!


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

def by_three(number):
  number=int(raw_input ("enter a number that is divisble by three"))
  if number % 3 == 0:
    return cube(number)
  else:
    print "enter a valid number"
  by_three()

how do i make sure this loops and asks to enter valid number the get the cube


#9

Hello guys, do you know where can I get the solution of this exercise?

Also - how can I print the results? I tried and it didn’t work :frowning:


#10

What code did you try?

You should get a result by printing the by_three function and inserting any number of your choosing inside the ().


#11

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


#12

I don’t understand why my code didn’t work.
Can anyone help me out?
I tried:

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

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

when I click run I get “cube(2) returned 2 instead of 8”

The solution they give is:

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

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

#13

return not True, why not simple use False?

your problem is here:

number ** 3

you do a math operation, but you do nothing with the result of the math operations, we can prove this:

num = 5
num ** 3
print(num) # output: 5
num = num ** 3 # update num variable with result of math operation
print(num) # output: 125

#14

I thought when I write return number,
the result of number ** 3 gets stored in number


#15

no, you don’t update the number variable. You can of course return the result of the math operation directly (which the solution is doing) without intermediate step.


#16

so after I define a function, then after the : on line 2 I write something to do (ie: number **3 ), and on line 3 I write return number
the result of line 2 does not get stored in the variable following return in line 3


#17

yes, i even demonstrated this with an example:

num = 5
num ** 3
print(num) # output: 5
num = num ** 3 # update num variable with result of math operation
print(num) # output: 125

with comments which are hopefully useful and should be of aid to you


#18

I appreciate your time in helping me straighten out my confusion.
Your example I understand. What it does not address is “return”.
I would like to correct my thinking, so I’m asking, does using return number
store the result of the computation that is above return?


#19

no, return is what the function is handing back:

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

print cube(3) # use print to print the returned result
x = cube(5) # store the returned result in a variable named x
print x # output: 125

return allows us to hand back data at the end of the function.


#20

Thank you. Much appreciated