vendredi 19 avril 2019

I have a function that is supposed to create a list that is n long and contains random integers?

I am trying to create a function that two arguments; num_of_elements and up_to. the function should create list that is num_of_elements long and the elements are random numbers between 1 and up_to. And the list should be returned, and is within my controller class.

import random
class Controller:

    list = [] # is the list of elements that will be used by the searching and sorting algorithms

    def initialize_list_elements(self, num_of_elements, up_to):
        for _ in range(num_of_elements):
            list.append(random.randint(1, up_to))
        return list

# Test
controller = Controller()
print('list elements {}'.format(controller.initialize_list_elements(10, 20)))


expected result is a list that is n long and each element is random between 1 and m but I'm getting the error,

TypeError: descriptor 'append' requires a 'list' object but received a 'int'




Aucun commentaire:

Enregistrer un commentaire