Function issue in Lesson 7


#1

Here is the problem I am working on & getting error message. Any help would be greatly appreciated. Below the problem is my code that I have written & the error message.

Problem

First, def a function called cube that takes an argument called number. Don't forget the parentheses and the colon!
Make that function return the cube of that number (i.e. that number multiplied by itself and multiplied by itself once again).
Define a second function called by_three that takes an argument called number.
if that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False.

Don't forget that if and else statements need a : at the end of that line!

Code

def cube(number):
    return 3**3
def by_three(number):
    if %d divided by %d is %d, % (cube, by_three)
        return False

Error Message

File "python", line 4
if %d divided by %d is %d, % (cube, by_three)
^
SyntaxError: invalid syntax


Replace this line with your code.


#2

This will always return 27. Not much of a function. It ignores the argument.

Try this,

def cube(number):
    return number ** 3

This has a string formatting expression that is not quoted, so invalid, but is also not a valid conditional expression. It is intended for output.

if that number is divisible by 3, by_three should call cube(number) and return its result.

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

#3

your if statement contains a few flaws.

the if statement is to determine wether or not the number is divisible by 3, agree?

How do we achieve this? we can use the module operator (%) it has been covered, do you remember how it works? Take a look at this code:

x = 4
if x % 2 == 0:
   print True

this will check if the remainder of x is zero. As you can see, i am comparing with 0 using two equal signs.

You can use the same trick for your problem.

If the number is divisible by 3, return the cube of the number (call cube with parameter number), else return False