Hi,
I am trying to print 20 5 digit random numbers.
class TestRandom:
arr=[ ]
def __init__(self, n):
self.n = n
def generate1000_5digit_RandomNum(self):
for i in range(n):
num = random.randint(10000, 99999)
TestRandom.arr[i] = num
for i in range(n):
print(TestRandom.arr[i])
def main(self):
self.genrate1000_5difit_RandomNum()
if __name__ == "__main__":
objTestRandom = TestRandom(20)
objTestRandom.main()
I am getting following errors:
Traceback (most recent call last):
File “/home/zulfi/PycharmProjects/sort/20_5digitRandom.py”, line 17, in
objTestRandom.main()
File “/home/zulfi/PycharmProjects/sort/20_5digitRandom.py”, line 13, in main
self.genrate1000_5difit_RandomNum()
AttributeError: ‘TestRandom’ object has no attribute ‘genrate1000_5difit_RandomNum’
As @alkapwn3d mentioned, not a good idea to initiate a class with a variable.
Your error is actually finding fault with your typos. You have at least 2.
You took the time to define self.n, but refuse to use it…
This TestRandom.arr[i] = num will not work, because, even is defining arr within TestRandom, you would be assigning num to non-existent indices of arr.
Hi,
I have improved the code but I can’t figure out how to pass ‘n’.
import random
class TestRandom:
arr=[ ]
def __init__(self, n):
self.n = n
def generate1000_5digit_RandomNum(self, n):
for i in range(n):
num = random.randint(10000, 99999)
TestRandom.arr[i] = num
for i in range(n):
print(TestRandom.arr[i])
def main(self, n):
self.generate1000_5digit_RandomNum(n)
if __name__ == "__main__":
objTestRandom = TestRandom(20)
objTestRandom.main(20)
However, I am getting following errors:
Traceback (most recent call last):
File “/home/zulfi/PycharmProjects/sort/20_5digitRandom.py”, line 19, in
objTestRandom.main(20)
File “/home/zulfi/PycharmProjects/sort/20_5digitRandom.py”, line 15, in main
self.generate1000_5digit_RandomNum(n)
File “/home/zulfi/PycharmProjects/sort/20_5digitRandom.py”, line 11, in generate1000_5digit_RandomNum
TestRandom.arr[i] = num
IndexError: list assignment index out of range
import random
class TestRandom:
# arr = [] // this is a class variable. not meant to be modified
# def __init__(self, n):
def __init__(self):
# self.n = n // not necessary. instance is more usable without it
self.arr = [] # this is an instance variable. meant to be modified
# self refers to the parent object
# def generate1000_5digit_RandomNum(self, n):
# use camelCase or snake_case but not both
def generateRandomNumber(self, n):
# a good method name says exactly what it does
# python docstrings can come in handy
'''
Generates a random 5 digit integer and appends it to a list
@params n: number of integers to generate
'''
for i in range(n):
num = random.randint(10000, 99999)
# self.arr is an empty list. use append() to add num to your list after each loop
# self.arr[i] = num // doesnt work because your empty list has no index to refer to as i
# hence the error: list assignment index out of range
self.arr.append(num)
print(num) # print num atfer it is appended to list. no need for new loop
# for i in range(n):
# print(self.arr[i])
def main(self, n):
# self.generate1000_5digit_RandomNum(n)
self.generateRandomNumber(n)
if __name__ == "__main__":
objTestRandom = TestRandom()
# objTestRandom.arr // returns empty list
objTestRandom.main(20)
# objTestRandom.arr // returns [your, random, numbers]
Thanks for helping me. God bless you.
Last time I used indexes with array. I created the following code:
class TestSort:
arr = [4, 9, 12, 3, 5, 24] # class var access by className.arr
def __init__(self, n):
self.n = n
def sorting(self):
for i in TestSort.arr:
for j in range(len(TestSort.arr)-1):
if (TestSort.arr[j] > TestSort.arr[j + 1]):
temp = TestSort.arr[j]
TestSort.arr[j] = TestSort.arr[j + 1]
TestSort.arr[j + 1] = temp
the array in your TestRandom class was empty TestRandom().arr = []
the array in your TestSort class has 6 items TestSort().arr = [4, 9, 12, 3, 5, 24]
# Run this whole script in your editor
class TestIndexInArray:
def __init__(self, arr):
'''
@param arr: array to be tested
'''
self.arr = arr
def printItemsIndex(self):
'''
Prints the index of each item in a given array
'''
# message to tell us when the function was called
print(f'Print indexes for {self.arr}:')
for i in self.arr:
# find the index of i in arr
itemIndex = self.arr.index(i)
print(itemIndex)
print('\n')
def findItemAtIndex(self, index):
'''
@param index: refers to the location of the item in the array
'''
print(f'Item at {index} in {self.arr}:')
try:
print(self.arr[index])
except IndexError:
print(f'> error: index at {index} does not exist')
print('\n')
if __name__ =="__main__":
def header(num):
print(f'Test #{num}\n' + '='*40 + '\n')
header(1)
arr_one = TestIndexInArray([4, 9, 12, 3, 5, 24])
arr_one.printItemsIndex()
arr_one.findItemAtIndex(0)
arr_one.findItemAtIndex(2)
arr_one.findItemAtIndex(5)
arr_one.findItemAtIndex(6)
header(2)
arr_two = TestIndexInArray([])
arr_two.printItemsIndex()
arr_two.findItemAtIndex(0)