Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RandomNumbersArray created by vishwaraj00 - https://repl.it/@vishwaraj00/RandomNumbersArray
"""
Write a Python program that generates a list containing integers of random values. The program should take in user inputs for the list length and range of integer values. User-defined functions should be used in the program. The program should print out:
1. The generated list
2. List length
3. Largest integer
4. Smallest integer
5. Total sum of the integers in the list
6. Average of the integers in the list
The following demonstrates an example of an output:
Please enter the length for the list: 10
Please enter the lower bound for the range of integer values: -10
Please enter the upper bound for the range of integer values: 90
Sequence: [88, 36, 27, 59, 27, 8, 54, 59, 68, -4]
List length: 19
Largest integer: 88
Smallest integer: -4
Sum: 422
Average: 42.2
"""
import random
def randomNumber(lowerBound, upperBound):
if lowerBound > upperBound:
raise ValueError("lower bound cannot be greater than upper bound.")
# return a random number between lowerBound and upperBound
return random.randint(lowerBound, upperBound);
def createList(length, lowerBound, upperBound):
if length <= 0:
raise ValueError("Length should be a positive number.")
# create a list of size n
customList = [0] * length
# populate list with random numbers
for i in range(length):
customList[i] = randomNumber(lowerBound, upperBound)
# return the list
return customList
def printListDetails(list):
print(list.__str__())
print("List length: " + str(len(list)))
print("Largest integer: " + str(max(list)))
print("Smallest integer: " + str(min(list)))
print("Sum: " + str(sum(list)))
print("Average: " + str(sum(list)/(1.0 * len(list))))
def main():
length = int(input("Please enter the length for the list: "))
lowerBound = int(input("Please enter the lower bound for the range of integer values: "))
upperBound = int(input("Please enter the upper bound for the range of integer values: "))
customList = createList(length, lowerBound, upperBound)
printListDetails(customList)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment