Functions 6/19, help with code


#1

Here are the instructions:

1.First, def a function called cube that takes an argument called number. Make that function return the cube of that number

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.

Here’s my code:

def cube(number):
  number ** 3
  return number

def by_three(number):
  if number / 3:
    cube(number)
    return
  else:
    return False

Here’s the error message:
Error message “cube(2) returned 2 instead of 8”, what am I doing wrong here?

I know I’m doing something wrong but can’t work out what it is.

Edit: I’ve also tried:

def by_three(number):
  if number % 3 == 0:

#2
cube(number)
    return

what does it return?

instruction:

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


#3

Hey!
instead of

number ** 3
  return number

write

return number ** 3

or

number = number ** 3
  return number

and instead

 if number / 3:
    cube(number)
    return

try

 if number / 3:
   return  cube(number)

should work out well


#4

if number / 3:

It’s not right way to check condition whether or not the number evenly divisible by 3.

if 5/3: # 1 is True
  print True

the condition will be True.because any non-zero integer but 0 is considered as True.

so for checking whether or not the number evenly divisible by 3 we can use % operator.

if number % 3 == 0:

if there is no remainder(means 0) then it means the number is evenly divisible by 3 .


#5

Right!
I just gave a clue for indentation and syntaxis :upside_down_face:


#6

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