#๐ why would this cause a "list out of index" error?
33 messages ยท Page 1 of 1 (latest)
@mental yoke
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
This is going to cause a lot of problems
!e
nums = [[0] * 5 for i in range(8)]
print(nums)
@quasi wraith :white_check_mark: Your 3.12 eval job has completed with return code 0.
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Are there 40 elements in splitList?
yeah
it might look like a 5x8 board
im not that familar with python as i am with java, this is my first time making a 2d list
wait nevermind, this is ok, sorry
but there doesnt seem to be an issue with it
I thought the list comp was duplicating the list
when i decreased row and col by one, it filled everything within nums except for the last 8 values in which they were still 0s
boardValues = "6 4 256 16 32 4 16 64 4 8 64 64 256 8 16 2 2 256 4 32 128 2 64 8 256 32 128 16 2 8 64 64 128 32";
splitList = boardValues.split(" ")
nums = [[0]*5 for i in range(8)]
splitIndex = 0
for row in range(8):
for col in range(4):
nums[row][col] = int(splitList[splitIndex])
splitIndex += 1
print(nums)
!e
boardValues = "6 4 256 16 32 4 16 64 4 8 64 64 256 8 16 2 2 256 4 32 128 2 64 8 256 32 128 16 2 8 64 64 128 32";
splitList = boardValues.split(" ")
nums = [[0]*5 for i in range(8)]
splitIndex = 0
for row in range(8):
for col in range(4):
nums[row][col] = int(splitList[splitIndex])
splitIndex += 1
print(nums)
@quasi wraith :white_check_mark: Your 3.12 eval job has completed with return code 0.
[[6, 4, 256, 16, 0], [32, 4, 16, 64, 0], [4, 8, 64, 64, 0], [256, 8, 16, 2, 0], [2, 256, 4, 32, 0], [128, 2, 64, 8, 0], [256, 32, 128, 16, 0], [2, 8, 64, 64, 0]]
no error
boardValues = "6 4 256 16 32 4 16 64 4 8 64 64 256 8 16 2 2 256 4 32 128 2 64 8 256 32 128 16 2 8 64 64 128 32";
splitList = boardValues.split(" ")
nums = [[0]*5 for i in range(8)]
splitIndex = 0
for row in range(8):
for col in range(5):
nums[row][col] = int(splitList[splitIndex])
splitIndex += 1
print(nums)
!e
boardValues = "6 4 256 16 32 4 16 64 4 8 64 64 256 8 16 2 2 256 4 32 128 2 64 8 256 32 128 16 2 8 64 64 128 32";
splitList = boardValues.split(" ")
nums = [[0]*5 for i in range(8)]
splitIndex = 0
for row in range(8):
for col in range(5):
nums[row][col] = int(splitList[splitIndex])
splitIndex += 1
print(nums)
@mental yoke :x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 10, in <module>
003 | nums[row][col] = int(splitList[splitIndex])
004 | ~~~~~~~~~^^^^^^^^^^^^
005 | IndexError: list index out of range
!e
boardValues = "6 4 256 16 32 4 16 64 4 8 64 64 256 8 16 2 2 256 4 32 128 2 64 8 256 32 128 16 2 8 64 64 128 32";
print(len(boardValues.split()))
@quasi wraith :white_check_mark: Your 3.12 eval job has completed with return code 0.
34
there's only 34 numbers in your boardValues string
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.