Practice makes perfect


Replace this line with your code.


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


The above code is returning the error :
Oops, try again. cube(1) returned 'number*number*number' instead of 1
Can someone tell me why?


There is a difference between data types like strings that are usually wrapped with "" and numbers which aren't wrapped in "". You seem to be getting data types mixed up. "number*number*number" is just a string and won't give you the cube. Same with return "cube(number)". You're just returning a string.

Also, perhaps you should review various mathematical operations such as **.
** is meant for exponents. Why not use **? You can use it to get the cube in a clean way rather than number*number*number.

>> 2**4
>> 16

Remember, this is Python and not Javascript, you don't necessarily need brackets in this line:


guys this worked for me

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

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


Nope, this did'nt work for me, it says "cube(1) returned 3 instead of 1"
But I added a raw input as I dont know where the "number" is supposed to come from?

def cube(number):
cube = number*3
return cube
def by_three(number):
by_three = number % 3
if number % 3 == 0:
return cube(number)
return False
number = raw_input("add a number")


The number is a user input. Codecademy has tests behind to see if your code passes. Outside of Codecademy, you test your function by calling it like this:

by_three(4) #Will not show in console though
print by_three(4) #Will show in console

What does this do? You are assigning the function name to something else?


Thanx, this is very confusing. I'm trying to actaully figure out Pythin asap to finish my semester.
Next I need to build a conversion program in PyQt and I dont even know where you put code into that :frowning:


I'm not too familiar with PyQt :sweat:
Good luck though! Reading their docs and exploring is what I'd suggest doing to get familiar with it.


Thanx @datfatcat. I'm struggling sooo much. Will probably end up cancelling this module

Groete / Regards
Lindi de Beer


To confirm kirancoding04's reply, this is working for me. I struggled with the first function... Was using 'number' instead of 'cube'...

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


Are the brackets around ' number % 3 == 0 ' necessary?


No, it isn't necessary :slight_smile:


Cool, thanks!

Dennis Mithamo.

0722 202 843


I got it, my code was not working too, I guess I didn't create a variable cube, only wanted it to return...thanks though


Actually, you don't need to create any variable called cube. You can simply do return number ** 3. Makes the code more concise.


guys, this works for me.

def cube(number):
    return number ** 3

def by_three(number) :
    if number % 3 == 0 :
        print "cube is divisible by 3"
        return cube(number)
    else :
        print "cube is not divisible by 3"
        return False


Oh great if so then its cool...thanks


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.