I'm new with Python and coding, doing some exercises online.
I am writing a class for making lists of random numbers:
class RandomList:
def create_list(self):
self.list = []
def fill_list(self, min, max, range):
self.list = [random.randint(min, max) for i in range(range)]
First of all -->
I do not understand the Pycharm warning message under self.list = []
telling me:
"Instance attribute list is defined outside init"
What would be the difference if I wrote:
class RandomList:
def __init__(self):
self.list = []
Secondly -->
The fill_list function doesn't work when I call RandomList.fill_list
:
TypeError: 'int' object is not callable.
And there is a warning message under 'i':
Local Variable not used
I don't understand why
I am at loss here because when I use for example:
a = [random.randint(min, max) for i in range(range)]
outside of the class there is no issue whatsoever. I think I must be mixing things up here...
First: You can not use variable names with min, max, range
Example:
range = 4
for i in range(range): #you will get error
print("Hello!")
Your code should be
class RandomList:
def create_list(self):
self.list = []
def fill_list(self, minnum, maxnum, rangenum):
self.list = [random.randint(minnum, maxnum) for i in range(rangenum)]
And you can test it with
randlist = RandomList
randlist.create_list(randlist)
randlist.fill_list(randlist, 1, 5, 3)
print(randlist.list) #prints randomly like [1, 4, 3]