Is this normal?


#1



https://www.codecademy.com/courses/python-beginner-c7VZg/1/5?curriculum_id=4f89dab3d788890003000096


I'm having issue completing this part.


Is this normal that I'm having some serious trouble and is about to get some help? Or should I solve it myself to be good at this and practice myself?


Replace this line with your code. 
def cube(number):
    return number**number
def by_three(number):
    if number % 3 == 0:
        print "%d is divisible by 3" % (number)
    else:
        print "%d is not" % (number)
        
cube(6)
by_three(8)


#2

Is this normal that I'm having some serious trouble and is about to get some help? Or should I solve it myself to be good at this and practice myself?

That's how we learn,the more obstructions ,we need to push more and find what we don't know and keep doing it by taking help of tutorials,people etc.

Now back to your code..

in this exercise we have to define a function cube which takes a number and returns cube of of that number ..

so how we find cube of a number?
two ways..

number * number * number

or we already learnt about a operator **
we can use it ... number ** 3.

Now the second part we have to define a function that checks if a number is divisible by 3 or not, if it is then we need to return cube of that number, we can again use this..

number * number * number

or we already learnt about a operator **
we can use it ... number ** 3.

But we have defined a function to do it?
so we can directly use our helper function ( cube ).

then in else-part..
if its not divisible by 3 then we have to return False!


#3

Thanks for the quick reply. But I found it out myself before I get to see your reply ! I'm very happy lol

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

by_three(6)


#4

:thumbsup: , I hope the code is clear to you Or we can discuss about it :slight_smile:


#5

I'm not very sure how the code works as Im not allowed to test it again with different input.

PS
I'm trying to figure it out atm. Hehe thank you! hope to see you around.


#6

The testing is done by CC's interpreter,

We can install python IDLE and test out the code
or
can use online python interpreter like This


#7

Great! Btw one question. What does this line do with the code?
return cube(number)

PS.
from my knowledge it returns the cube("""myinput which is"""6)


#8

It's a return statement.

return is a keyword in python, when we call a function ,it simply hands over the value attached right side of it( here the value is cube(number) , Points to note..

1.return only makes sense inside a function.
2.also when function reaches it,it returns the value and halts the execution, code after return not get a chance to get executed, if there is any code after it.

cube(number)
In this part we're using already defined function cube() that calculates the cube of a number, Function basically saves us from lots of work.


#9

I get it now! Thanks alot :slight_smile:


#10

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