In: Computer Science
Write a program(Python) using functions and mainline logic which prompts the user to enter a number. The number must be at least 5 and at most 20. (In other words, between 5 and 20, inclusive.) The program then generates that number of random integers and stores them in a list. The random integers should range from 0 to 100. It should then display the following data to back to the user with appropriate labels:
The list of integers
The lowest number in the list
The highest number in the list
The total sum of all the numbers in the list
The average number in the list
Use try/except to make sure the user enters an integer.
[Make it simple so that i can understand]
Thanks for the question.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks!
===========================================================================
import random
def main():
N = 0
# The number must be at least 5 and at most 20
while N < 5 or N > 20:
# Use try/except to make sure the user enters an integer
try:
N = int(input('Enter a number between 5 and 20 (inclusives): '))
except:
print('Error: Please enter only integers.')
# generates that number of random integers and stores them in a list.
random_list = [random.randint(0, 100) for _ in range(N)]
# The lowest number in the list
print('Lowest Number:', min(random_list))
# The highest number in the list
print('Highest Number:', max(random_list))
# The total sum of all the numbers in the list
print('Sum of all numbers:', sum(random_list))
# The average number in the list
average = sum(random_list) / N
print('Average of all numbers:', average)
if __name__ == '__main__':
main()
============================================================

