Python Forum

Full Version: Calling a function from another function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Having trouble with this one.
The problem reads below:

You will need to write two functions for this problem. The first function, "divide" that takes in any number and returns that same number divided by 2. The second function called "sum" should take any number, divide it by 2, and add 6. It should return this new number. You should call the divide function within the sum function. Do not worry about decimals.

Here's my attempt:
def divide (div):
    d2 = div/2
    print(d2)
    return d2
divide(10)

def sum (num):
    s1 = divide(num)
    s2 = s1 + 6
    print(s2)
    return s2

num = 12
Here's the output:
Output:
5.0
It seems to execute "divide" but not "sum".
Not quite sure why.
what is wrong? You are invoking divide(10), and the function returns 5, as expected. Just call sum(12) or sum(num), and you get what you want.

Note about naming: sum is not a good name for a new function. Python already has sum as a builtin function.
You only call divide. If you want sum executed, you need to call it.