statistics are often calculated with varying amounts of input data. write a program that takes any number of integers as input and outputs the average and max

You have to write a python program to accept n number of integers from the user, using input function call and then convert it to integer and store it into a list.

 

statistics are often calculated with varying amounts of input data. write a program that takes any number of integers as input, and outputs the average and max

 



Once you get the list of integers from the user, find the sum of all integers by iterating all the list items.

Get the list length using the len() function call and then divide the sum of integers by length to get the average.



write a program that takes any number of integers as input, and outputs the average and max

Take an empty list of numbers and using a while loop ask the user to enter a number, until enter is pressed.


number_list = []
while True:
    number = input("\nEnter a Number (Press Enter to Exit Loop) : ")
    if number == "":
        break
    else:
        number_list.append(int(number))




Take the new variable total and assign zero to it. Use the for loop and iterate over all the list of numbers and add all the numbers  to total. Once the for loop completes the total variable should have stored the sum of all the numbers in a list.

total = 0

for item in number_list:
    total += item




Take another variable named as average and divide total by list length, this will give us the average of numbers a floating point variable.


Finally print the sum and average of list using print function call.

average = total / len(number_list)

print(“Sum of all the numbers is :” , total)
print(“Average of all the numbers is %.2f :”  % average)



Final program
=================================

number_list = []
while True:
   number = input("\nEnter a Number (Press Enter to Exit Loop) : ")
   if number == "":
       break
   else:
       number_list.append(int(number))

total = 0

for item in number_list:
   total += item

average = total / len(number_list)

print("Sum of all the numbers is :" , total)
print("Average of all the numbers is %.2f : "  % average)




OUTPUT:
====================

Enter a Number (Press Enter to Exit Loop) : 1
Enter a Number (Press Enter to Exit Loop) : 2
Enter a Number (Press Enter to Exit Loop) : 3
Enter a Number (Press Enter to Exit Loop) : 4
Enter a Number (Press Enter to Exit Loop) : 5

Enter a Number (Press Enter to Exit Loop) :
Sum of all the numbers is : 15
Average of all the numbers is 3.00 : 




Conclusion : Try running a program, comment down below if you have any queries.

Keywords :

statistics are often calculated with varying amounts of input data. write a program that takes any number of integers as input, and outputs the average and max

 

Post a Comment

0 Comments