In: Computer Science
Do it in python please
Write a program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a list. It should then display the following data to back to the user:
Helpful hint: don't forget about input validation loops and try/catch exceptional handling. Both are very useful when used in conjunction with functions.
Source Code:
import random
def random_list(n):
rand_list=[]
for i in range(0,n):
rand_list.append(random.randint(0,100))
return rand_list
def getMinimum(lst):
return min(lst)
def getMaximum(lst):
return max(lst)
def getSum(lst):
return sum(lst)
def getAverage(lst):
return sum(lst)/len(lst)
n=int(input("Enter the number of random numbers(must
greater than 0):"))
while(n<=0):
print("Sorry please enter valid positive integer.")
n=int(input("Enter the number of random numbers(must greater than
0):"))
lst=random_list(n)
print("Generated Random List =",lst)
lowest=getMinimum(lst)
print("Minimum of List =",lowest)
highest=getMaximum(lst)
print("Maximum of List =",highest)
total_sum=getSum(lst)
print("Sum of the List =",total_sum)
average=getAverage(lst)
print("Average of List =",average)
Sample input and output: