6. Practice Makes Perfect

Hello All and thanks in advance,
attached is a picture of my code
the code appears to run, I tested it in IDLE, no syntax errors returned
if (n) isn’t defined then how am I supposed to get the answer 27?
I’m probably missing something here… not sure
again thanks for the help

also if I
return 27
else:
return False
then I get the error code “returned 27 instead of 216”

Hi @mrscamps ,

Your entire by_three function is indented within the cube function. Remove one level of indentation from each line of the by_three function to make it global. It could execute the way it is, but attempting to use the by_three function would be more complex than it needs to be.

In the if block, you have …

return True

… but should have …

return cube(n)

If you add the following line at the end of your code, after correcting the indentation, you should output 27, because 3 gets assigned to n during the function call …

print by_three(3)

Do not indent the print statement.

2 Likes

Thanks! it worked
sorry still confused though.
at what point did n = 3??
if you input 3 into the program it all makes sense
but I can’t see where n became 3

also for anyone else needing help here’s completed status.

2 Likes

Hi @mrscamps ,

As noted above, if you do this, n will be assigned the value, 3

print by_three(3)

But even if you do not do that, Codecademy tests your function behind the scenes, as that is where the original 3 came from.

thanks it worked :relaxed:
but im not getting the flow of the code …

Didn’t work for me, used exactly the same code and it didn’t work :frowning:
Indents are all in the correct place
def cube(n):
return n (n*n)
def by_three(n):
if (n) % 3 == 0:
return cube(n)
else:
return False
print by_three(n)

1 Like

hey a few mistakes that i found in your code was that on line two of your code is supposed to look like this.
return n * (n*n)
and on line three and four.
print " %d cubed is %d." %(n, cubed)
return cubed
and on your last line put a 3 inside the parentheses instead of a n.
print by_three(3)

Hello group!
This is my first posting, and I ve come here desperately…Can anyone help me with this?
The ‘Prof’ says it’s all good, but I’m wondering why my console does not display as required by the if/else results.

def cube(number):
return (number**3)
def by_three(number):
if (number % 3) == 0 and number >=3:
return cube(number)
print “Number is divisible by three”
else:
print “n is not”
return False

BTW: My indentations were pasted aligned left, but the program is formatted correctly.

help iv been messing around with this code for 2 hours cant get it to pass
def cube(n):
return n * (n*n)
def by_three(n)
if (n) % 3 == 0:
return cube(n)
else:
return False
print by_three(3)

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

You forgot to put the * after the return n

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

There the print shouldnt be at the very start it need to be indeted one

I think the code should look like this=

def cube(number):
return numbernumbernumber

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

print by_three(3)

maybe…
but “number” is not defined, your code not working

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

def by_three(number):
if (number) % 3 == 0:
return cube(number)
else:
return False
print by_three() #input your number here

I have been trying this one and have yet to figure this one out as well. I have my code formatted on the below link with the error and result. Not sure on why I keep getting by_three(3) returned 3 instead of 27
Imgur

def cube(number):
return number**3

def by_three(number):

if number % 3 == 0:
    return number
else:
    return False

by_three(9)
.

Any ideas? Thanks CodeAcademy.

This might work? all indented.

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

@killawatts13 you could ask for printing by_three
but we need to print cube(n) like so:

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

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

The correct syntax for this module is

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

you must define (n) when printing the statement which is 3.

1 Like

You can use the input command to test your code!!!

print ‘Rise a # to the power of 3 if divisible by 3’
number = input ('Enter a number: ')

def cube(number):
return number * number * number
def by_three(number):
if (number) % 3 == 0:
return cube(number)
else:
return ‘non divisible by 3’

print 'The number is %s ’ % (by_three(number))