6. Practice Makes Perfect


#1

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


#2

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


#3

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.


#4

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.


#5

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.


#6

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


#7

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)


#8

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)


#9

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


#10

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


#11

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)


#12

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


#13

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


#14

I think the code should look like this=

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

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

print by_three(3)


#15

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


#16

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.


#17

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


#18

@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


#19

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.