Write this program in python

Question:

Write a program that contains ADD and AVERAGE user defined functions. On execution your program prompts for three numbers from user and call the AVERAGE function. Send the entered three numbers to AVERAGE function. The AVERAGE function call ADD function and send the three user entered numbers to it. The ADD function accepts the numbers from AVERAGE function and calculate sum. Send this sum value back to calling point (AVERAGE function). The AVERAGE function receive the sum value and calculates average for that sum of three numbers. The AVERAGE function send the average value to its calling point (outside of both functions). At the end display the average value from outside these functions.

The output should be:

a: 2
b: 3
c: 4
Average: 3.0

Asked By: James Bond

||

Answers:

def add(a,b,c):    
    return a+b+c  

def average(a,b,c):
    d = add(a,b,c)
    e = d/3
    return e

f = average(2,3,3)
print(f)

Output:

f = 2.6666666666666665

Answered By: Rohit-Pandey

Generic way of doing this :

def adder(num):
    return sum(num)

def avg(*num):
    return adder(num)/len(num)


print("Average: ",avg(1,2,3,4))

Now you can pass as much numbers as you want.

Answered By: LMSharma

The best practice tactics will solve it, like;

n1 = int(input("Enter Number 1: " ))
n2 = int(input("Enter Number 2: " ))
n3 = int(input("Enter Number 3: " ))

def ADD(a,b,c):
return a+b+c  

def AVERAGE(a,b,c):
X = ADD(a,b,c)
Y = X/3
return Y

F = AVERAGE(n1, n2, n3)
print(F)

Good luck!

Regards: Khairullah Hamsafar

Answered By: Khairullah Hamsafar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.