pythoncompiler-errorsgalois-field

error 'int' object is not subscriptable python


i am learning python can you please help me with this code for galois field xor code is

def generateGF(a,b):
    for x in range(a**b-1):
        for y in range(a**b-1):
            a[x][y] = bin(x)[2:].zfill(3) + bin(y)[2:].zfill(3) # limited for 2^3
    for i in range(a**b):
         for j in range(a**b):
             print(bin(z[i][j]),end=' ')
         print("\n")
print (generateGF(2,3))

and i getting this error

python lab5.py                             :(
Traceback (most recent call last):
  File "lab5.py", line 9, in <module>
    print (generateGF(2,3))
  File "lab5.py", line 4, in generateGF
    a[x][y] = bin(x)[2:].zfill(3) + bin(y)[2:].zfill(3) # limited for 2^3
TypeError: 'int' object is not subscriptable

Solution

  • The problem is you have too many expressions on one line so it's difficult to identify exactly which integer is being subscripted.

    Split up the expressions:

    a[x][y] = bin(x)[2:].zfill(3) + bin(y)[2:].zfill(3)
    

    is the same as:

    filled_x = bin(x)[2:].zfill(3)
    filled_y = bin(y)[2:].zfill(3)
    
    a[x][y] = filled_x + filled_y
    

    Then you'll know which subscript is causing the error.

    (Actually, technically you could need to split up a[x][y] as well, but from your test data we can see a is 2, so that's what's causing the problem)