135k views
5 votes
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:

A. The list of integers
B. The lowest number in the list
C. The highest number in the list
D. The total sum of all the numbers in the list
E. The average number in the list
F. At a minimum, the numbers should range from 0 to 50, but you can use any range you like.

User Barnabas
by
6.8k points

1 Answer

4 votes

Final answer:

The program creates a list of random integers based on user input and displays statistics about the list, such as the lowest number, highest number, sum, and average of the integers.

Step-by-step explanation:

Writing a Program to Generate Random Integers

To write a program that generates a list of random integers based on user input, we can use Python's random module. First, we need a function to create the list of random integers. Then, we can print the required statistics based on the generated list. Below, a simple Python program is outlined which accomplishes this task:

import random

def generate_random_integers(n, min_val=0, max_val=50):
return [random.randint(min_val, max_val) for _ in range(n)]

def main():
num = int(input('Enter the number of random integers you want to generate: '))
random_integers = generate_random_integers(num)
print('A. The list of integers:', random_integers)
print('B. The lowest number in the list:', min(random_integers))
print('C. The highest number in the list:', max(random_integers))
print('D. The total sum of all the numbers in the list:', sum(random_integers))
print('E. The average number in the list:', sum(random_integers)/len(random_integers))

if __name__ == '__main__':
main()

This program first defines a function to generate a list of random integers, and then it executes the main function, which displays the list and the calculated statistics to the user.

User RobinJ
by
7.9k points