Write a function called createList that will create and fill a list of any size with random numbers in the range of 1 - 100. This function should take the size of the list as a parameter to the function. It should return the created list.

Respuesta :

Answer:

The solution is written in Python code:

  1. import random  
  2. def createList(size):
  3.    newList = []
  4.    for i in range(size):
  5.        newList.append(random.randint(1,101))
  6.    return newList

Explanation:

Since we need to generate random number, we import random module to our program (Line 1).

Next we create a function createList that takes one input parameter, size (Line 2).

In the function body, we create an empty list, newList (Line 3).

We create a for-loop to iterate for size number of rounds and in each iteration generate a random number between 1 -100 using the randint method from the random module and append it to the newList (Line 4-5).

At last return the newList as function output (Line 7).