Practice Makes Perfect


#1

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
   else:
#outcome if the condition is false
        return False

#2

Hey,can you format code ?

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


#3

@newb528
In this line of code..
Problem1.

def by_three(number):
    return number % 3

You do not have to return number % 3 ?

hint*
read the instruction

Problem2.

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.

Note..

def anyfunc():
    return 3
    #code
    #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

#code
#more code

never actually gets executed!

if by_three(number) == 0:

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

Example..

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

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


#4

Right below is what I worte

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

#5

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