I'm getting an error by codecademy that says: cube(2) returns 4 instead of 8


#1

Hey I'm having a error I don't know how to fix.
This is my code:

def cube(n):
return n**2

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

I'm getting an error by codecademy that says: cube(2) returns 4 instead of 8.

Any help is much appreciated. Thank you


#2

Hey I fixed my error but now I'm getting a new error:
File "python", line 10, in
File "python", line 2, in cube
NameError: global name 'n' is not defined
The new code is here:
def cube(number):
return n**3

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

cube(2)

Help is much appreciated thank you.


#3

that didn't quite work for me, i get the error
Traceback (most recent call last):
File "python", line 9, in
File "python", line 2, in cube
NameError: global name 'n' is not defined


#4

Yeah I'm getting the exact same error, I don't know what to do. Help please


#5

Try this


#6

Line 1 – def
cube(number): Line 1 defines a function named cube with an argument named number. It ends with a colon.

Line 2 – return
number **3 Line 2 is indented four spaces and returns the contents
of the variable number cubed.

Line 3 – def
by_three(number): Line three defines a function named by_three with the argument named number. It ends with a colon.

Line 4 – if number %3==0: Line 4 is indented four spaces. Line four is
an if statement that checks to see if the content of the variable number is divisible by three.
The % 3 ==0 is the modulo(%). Modulo divides the contents of number by three and checks for the remainder. If the content of the variable number has a remainder of 0 it will be == to 0 and is divisible by 3. If there is any remainder, the number would not be divisible by 3. The if statement ends with a colon.

Line 5 – return cube(number) Line 5 returns the result of the function named cube created in line 1. It has to be indented an additional 4 spaces.

Line 6 – else: Line 6 creates an else statement.

Line 7 – return False Line 7 will return the word False if the
content of the variable number is not divisible by three.

Line 8 – by_three(4) Line 8 calls the by_three function with the argument 4.

Running the code –

The program starts when the function is called in line 8. It
will place a 4 into the variable named number. Line 4 will replace the content of number
with a 4. Which will result in if 4 %3
==0. Since 4 divided by 3 has a remainder of 1 it is not equal to zero and
the program will move to line 6, the else
statement. Line 7 will print False.

If the content of line 8 was changed to by_three(9) , line 4 will
replace the content of the variable number
with a 9. Which will result in if 9 %3 ==0. Since 9 divided by 3 has a
remainder of 0 it is equal to 0 and the program will move to line 5 which will
call the cube(number) function
created in line 1 and return the cubed number.