python program, Implement the following three functions (you should use an appro
ID: 3766226 • Letter: P
Question
python program, Implement the following three functions (you should use an appropriate looping construct to compute the averages):
allNumAvg(numList) : takes a list of numbers and returns the average of all the numbers in the list.
posNumAvg(numList) : takes a list of numbers and returns the average of all the numbers in the list that are greater than zero.
nonPosAvg(numList) : takes a list of numbers and returns the average of all the numbers in the list that are less than or equal to zero.
Write a program that asks the user to enter some numbers (positives, negatives and zeros). Your program should NOT ask the user to enter a fixed number of numbers. Also it should NOT ask for the number of numbers the user wants to enter. But rather it should ask the user to enter a few numbers and end with -9999 (a sentinel value). The user can enter the numbers in any order. Your program should NOT ask the user to enter the positive and the negative numbers separately. Your program then should create a list with the numbers entered (make sure NOT to include the sentinel value (-9999) in this list) and output the list and a dictionary with the following Key-Value pairs (using the input list and the above functions):
Key = 'AvgPositive' : Value = the average of all the positive numbers
Key = 'AvgNonPos' : Value = the average of all the non-positive numbers
Key = 'AvgAllNum' : Value = the average of all the numbers
Sample run: Enter a number (-9999 to end): 4 Enter a number (-9999 to end): -3 Enter a number (-9999 to end): -15 Enter a number (-9999 to end): 0 Enter a number (-9999 to end): 10 Enter a number (-9999 to end): 22 Enter a number (-9999 to end): -9999 The list of all numbers entered is: [4, -3, -15, 0, 10, 22] The dictionary with averages is: {'AvgPositive': 12.0, 'AvgNonPos': -6.0, 'AvgAllNum': 3.0}
Explanation / Answer
See the below Python program for calculating avg of three types.
I have defined three functions one is allPositiveAvg, allNegtiveAvg and allTotalAvg are below..I am using list for to read the values from the console values[] and I defined three list for a[] for all, p[] for positive and n[] for negitve values and these list are used to storing the values ..and after storing the values in each list will pass the argument to a corresponding function..then function will calculate the return average value..
See the below code for above given problem ..
a = [] # all
p = [] # pos
n = [] # neg
print("Enter positive, negative or zero to determine the average: ")
values = []
for i in iter(lambda: int(input("Enter a number (-9999 to end): ")), -9999):
if i >= 0: # if positive append to p
p.append(i)
else: # else must be negative so append to n
n.append(i)
a.append(i) # always add to a to get all nums
def allNumAvg(values):
return sum(values) / len(values)
def posNumAvg(values):
return sum(values) /len(values)
def nonPosAvg(values):
return sum(values) / len(values)
print("The list of all numbers entered is:")
# pass correct list to each function
dictionary = {
"AvgPositive": posNumAvg(n),
"AvgNonPos": nonPosAvg(p),
"AvgAllNum": allNumAvg(a)
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.