Python Forum
Calling a function from another function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calling a function from another function
#1
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.
Reply
#2
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.
Reply
#3
You only call divide. If you want sum executed, you need to call it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  GCF function w recursion and helper function(how do i fix this Recursion Error) hhydration 3 2,485 Oct-05-2020, 07:47 PM
Last Post: deanhystad
  Passing a function to another function yulfmd1802 3 2,188 Jan-23-2020, 09:13 AM
Last Post: ndc85430
  Adding a sqrt function syntax error and <build-in function sqrt> from tkinter import Kael 0 1,826 Oct-14-2019, 05:51 PM
Last Post: Kael
  Need of return in function if statement inside the function already returns Athul 5 3,868 Aug-16-2018, 10:19 AM
Last Post: DuaneJack
  Calling function-- how to call simply return value, not whole process juliabrushett 2 3,168 Jul-01-2018, 01:17 AM
Last Post: juliabrushett
  Calling a Returned Value to Another Function valerydolce 9 6,597 Mar-28-2017, 09:54 PM
Last Post: valerydolce
  Calling a function to return a list of percentages Liquid_Ocelot 7 6,230 Mar-25-2017, 01:20 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020