6. Practice makes perfect in Python functions


#1



When I run the code I get "Oops, try again. by_three(1) returned None, did you remember to return the result? "


What am I doing wrong here?
Thanks

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


#2

Hi, @enigmaticgenius ,

In the else block of the by_three function, you are assigning False to the name, by_three, instead of returning False.


#3

Could you explain how 'if number % 3 == 0:' checks for the number being divisible by 5? I seem to have forgotten that part of the course.


#4

That definitely does not check if it is divisible by 5. that bit of code is checking div by 5. I don't know if that is the objective of the exercise, but that is what it is doing.


#5

So, you're not dividing by 3, you're finding the div. What you want to do is make sure that the number is both divisible by 3, and evenly divisible by 3. You're on the right track, because you do need a div, but you also need to divide. Create an and statement that checks the number for divided by 3 and that the div of it by 3 is 0. This makes sure that any number greater than 3 does not just pass through the code and get squared.

Also, putting the by_three = False makes the whole function false, so you want to make it return False instead.

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


#6

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