Practice Makes Perfect - Different Error


#1

I am confused on how to resolve my error and what the code is saying if I actually typed out the instructions.
My code is:
number = raw_input("Enter a number:")
def cube(number):
cube = number ** 3
return cube
def by_three(number):
if number / 3 >= 0:
return number
else return False

I entered 9 and the error was - by_three(1) returned 1 instead of False.

Initial Instructions:
First, def a function called cube that takes an argument called number. Don't forget the parentheses and the colon!
Make that function return the cube of that number (i.e. that number multiplied by itself and multiplied by itself once again).
Define a second function called by_three that takes an argument called number.
if that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False.


#2

The problem lies within this line. The answer can be found at this lesson: modulo

Codecademy runs by_three(1) to make sure the code is done dynamically rather than hard-coded. It is returning 1 because it is true that (1/3) >= 0. The lesson wants you to find out if a number is divisible by 3 (remainder of 0).

Hope this helps! If not, feel free to reply and I'll try to go more in-depth.


#3

Hi Kevin,

Thanks for the input! Could you give more detail? I am new to
monogramming and I misinterpret some instructions.


#4

Sure
In this assignment, you don't want to use the division sign because finding the quotient will not help you out with solving the problem. What will help though, is the modulo ( % ). The modulo divides to numbers and returns the remainder. When trying to find a number divisible by three, you want to find a number so that number % 3 == 0. It is the number divided by three but instead of returning the quotient, it returns the remainder.
So if number = 9, number % 3 will equal 0 because 9 / 3 = 3 with remainder 0
If number = 5, number % 3 will equal 2 because 5/3 = 1 with remainder 2

Hope this helps! If not, feel free to reply and I'll try to go more in-depth.


#5

Hi, someone help me solve my mistake?
number = raw_input("Enter a number:")

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

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

Oops, try again. by_three(3) returned 3 instead of 27


#7

We need to return the function call, which will in the end be the cubed number.

return cube(number)

#9

Thank you!! This was very helpful!


#12