#๐Ÿ”’ why would this cause a "list out of index" error?

33 messages ยท Page 1 of 1 (latest)

mental yoke
#

im having trouble understanding how line 10 causes this error

ebon flowerBOT
#

@mental yoke

Python help channel opened

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.

mental yoke
quasi wraith
#

This is going to cause a lot of problems

#

!e

nums = [[0] * 5 for i in range(8)]
print(nums)
ebon flowerBOT
#

@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]]
iron mountain
mental yoke
#

yeah

quasi wraith
#

it might look like a 5x8 board

mental yoke
#

im not that familar with python as i am with java, this is my first time making a 2d list

quasi wraith
#

wait nevermind, this is ok, sorry

mental yoke
#

but there doesnt seem to be an issue with it

quasi wraith
#

I thought the list comp was duplicating the list

mental yoke
#

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

quasi wraith
#

can you paste your code here?

#

!code

ebon flowerBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

mental yoke
#
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
#

!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)
ebon flowerBOT
#

@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]]
quasi wraith
#

no error

mental yoke
#
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)

ebon flowerBOT
#

@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
quasi wraith
#

!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()))
ebon flowerBOT
#

@quasi wraith :white_check_mark: Your 3.12 eval job has completed with return code 0.

34
quasi wraith
#

there's only 34 numbers in your boardValues string

mental yoke
#

bruh i forgot one number

#

alr i fixed it now

#

appricate the help

ebon flowerBOT
#
Python help channel closed

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.