Note that there are some explanatory texts on larger screens.

plurals
  1. POPython assignment index error
    primarykey
    data
    text
    <pre><code>def main(): rows = int(input("Insert # of rows ")) cols = int(input("Insert # of cols ")) dept = int(input("Insert # of dept ")) count = 0 trash = count table = [[[ 0 for i in range(cols) ] for j in range(rows) ] for k in range (dept) ] print () print ("Print empty table") print(table) print() print("Print each element") for i in range(rows): print("-" * 30, "rows", i+1) for j in range(cols): print("-" * 22, "col", j+1) for k in range (dept): count += 1 table [i][j][k] = count print ("row", i+1, "column", j+1, "depth", k+1, "=", count) print() print(table) main() </code></pre> <p>This works fine if all 3 numbers are the same. If they aren't, it stops the first time one of the variables has to go above it's designated number. ie. If 2, 3, and 2 are entered, it will work fine until it reaches the 3rd column and then stop, giving this message: </p> <pre><code>Traceback (most recent call last): File "&lt;pyshell#3&gt;", line 1, in &lt;module&gt; main() File "C:/Users/***/Documents/prog123", line 28, in main table [i][j][k] = count IndexError: list assignment index out of range </code></pre> <p>If I take out the line:</p> <pre><code>table [i][j][k] = count </code></pre> <p>It works fine, except it doesn't fill in the table. It also works fine if I make it only have two dimensions(taking out depth). Moving the line to earlier or later in the code doesn't seems to help either. I looked through similar questions on here already, but didn't see anything that helped me catch the error here.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload