Practice makes perfect


#1



Replace this line with your code.


#2

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

cube(9)

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


#3

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.
eg

>> 2**4
>> 16

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


#4

guys this worked for me

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

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


#5

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)
else:
return False
number = raw_input("add a number")


#6

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
#or
print by_three(4) #Will show in console

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


#7

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:


#8

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.


#9

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

Groete / Regards
Lindi de Beer


#10

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)
    else:
        return False

#11

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


#12

No, it isn't necessary :slight_smile:


#13

Cool, thanks!

Regards,
Dennis Mithamo.

0722 202 843


#14

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


#15

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


#16

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

#17

Oh great if so then its cool...thanks


#18

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