zak100 Posted March 11, 2020 Posted March 11, 2020 Hi, I am getting some problem with 'n'. Following is my code: 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) I am getting following error: Quote /home/zulfi/PycharmProjects/sort/venv/bin/python /home/zulfi/PycharmProjects/sort/20_5digitRandom.py Traceback (most recent call last): File "/home/zulfi/PycharmProjects/sort/20_5digitRandom.py", line 19, in <module> 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 = num IndexError: list assignment index out of range Process finished with exit code 1 Somebody please guide me. Zulfi.
Ghideon Posted March 11, 2020 Posted March 11, 2020 (edited) 8 hours ago, zak100 said: Somebody please guide me. It would help if you post the line numbers for the code when posting error messages referring to line numbers. Hints*: -What does "IndexError: list assignment index out of range" mean? -Does the variable arr contain an element at index i? *) Posted in Homework Help, no solution but hints will be given. Edited March 11, 2020 by Ghideon
zak100 Posted March 12, 2020 Author Posted March 12, 2020 (edited) Hi, I am storing the element in the array. Why index out of range? I am creating an unsized array. How to store elements in the array? Please guide me. Zulfi. Edited March 12, 2020 by zak100
Ghideon Posted March 12, 2020 Posted March 12, 2020 5 hours ago, zak100 said: Why index out of range? Because: (bold by me) 5 hours ago, zak100 said: I am creating an unsized array. If you try to access an element that does not exist, Pythons answer is index out of range. 5 hours ago, zak100 said: How to store elements in the array? Maybe you could try the list* functions in Python? There are plenty of references and tutorials online. *) Python supports lists, one of several available types of collections
zak100 Posted March 14, 2020 Author Posted March 14, 2020 Hi, Thanks for guidance. I am able to solve this problem. God bless you. Zulfi.
Ghideon Posted March 14, 2020 Posted March 14, 2020 16 hours ago, zak100 said: Thanks for guidance. I am able to solve this problem. Thanks for feedback! To help any other member that may be interested: If this line TestRandom.arr[i] = num Is changed to this: TestRandom.arr.append(num) It will print random numbers There may be requirements in the homework for writing the program as in OP but if the task is to get 20 random numbers it can be expressed more compact: import random for i in range(20): print(random.randint(10000, 99999))
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now