Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Beginner)Python functions Codeacademy

Tags:

python

I'm just learning to program on Codeacademy. I have an assignment, but cant figure out what I'm doing wrong.

First I need to define a function that returns the cube of a value. Then I should define a second function that checks if a number is divisible by 3. If it is I need to return it, otherwise I need to return False.

heres the code:

def cube(c):
    return c**3

def by_three(b):
    if b % 3 == 0:
         cube(b)
         return b
    else:
         return False
like image 341
LtC Avatar asked Dec 25 '22 23:12

LtC


1 Answers

You are not catching the return value of the function cube. Do b = cube(b). Or better yet, do return cube(b).

def cube(c):
    return c**3

def by_three(b):
    if b % 3 == 0:
         b = cube(b)
         return b  # Or simply return cube(b) and remove `b = cube(b)`
    else:
         return False

When you call the cube function with the argument b, it returns the cube of the passed argument, you need to store it in a variable and return that to the user, in your current code, you are neglecting the returned value.

like image 126
Sukrit Kalra Avatar answered Jan 07 '23 21:01

Sukrit Kalra