Practice Makes Perfect


Many have encounter this issue. What is the problem here?

def cube(number):
    return number**3
#define cube function
def by_three(number):
   return number % 3
#define the by_three function
   if by_three(number) == 0:
# Add the condition if the modulo is zero in the by_three
#function is divided evenly 
        return cube(number)
#outcome of the condition is true
#outcome if the condition is false
        return False


Hey,can you format code ?

1.Copy and paste you code here
2. Select all of your code
3. use </> to format it


In this line of code..

def by_three(number):
    return number % 3

You do not have to return number % 3 ?

read the instruction


If/else statement is inside the function and you're calling by_three() function inside function itself as if's condition ?...
Your if/else statement will never execute as your function halts code execution after it sees return statement and runs it.


def anyfunc():
    return 3
    #more code

As function sees the return statement ,it halts its execution and takes execution out of the body of function without running any code after return and

#more code

never actually gets executed!

if by_three(number) == 0:

You have to check if number is divisible by 3 or not.


If I had to check if a number is divisible by 2 or not.
I'd write something like this..
(If condition is inside If -statement)

if number % 2 == 0:
    #your code

here you have to check if number is divisible by 3?


Right below is what I worte

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


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