22.2k views
1 vote
Write a function called average() that takes a single parameter, a list of numeric values. The function should return the average of the values in the list. If the list is empty, average() should return 0.

>>>lst = [1,3,4]

>>>average(lst)

2.6666666666666665

>>>average([])

0

1 Answer

3 votes

Answer:

The following code is in python.

import statistics as st #importing statistics which include mean function.

def average(lst): #function average.

return st.mean(lst)#returning mean.

lst=a = list(map(int,input("\\Enter the list : ").strip().split()))#taking input of the list..

print("The average is "+str(average(lst)))#printing the average.

Output:-

Enter the list :

1 2 3 4 5 6

The average is 3.5

Step-by-step explanation:

I have used the statistics library which includes the mean function that calculates the mean or average of the list.In the function average having lst as an argument returns the mean of the list lst.

Then taking lst input from the user and printing it's average.

User Jay Jung
by
5.0k points