[Solved] Getting TypeError: ‘int’ object is not subscriptable [closed]


I suspect that’s what you’re looking for:

def hourglassSum():
    arr = []
    for i in range(0,6):
        arr1=map(int,input().split())
        arr.append(list(arr1))
    sum=[]
    for i in range(len(arr)-2):
        for j in range(min(len(arr[i]), len(arr[i+1]), len(arr[i+2]))-2):
            sum.append(arr[i][j]+arr[i][j+1]+arr[i][j+2]+arr[i+1][j]+arr[i+2][j]+arr[i+2][j+1]+arr[i+2][j+2])

    maxi=max(sum)
    print(maxi)

Two notes:

(1) you want to persist each iteration of the first loop, what you were doing was just overwriting your variable with each iteration

(2) while iterating across different elements of elements of your base list – always make sure you won’t run into index out of bounds exception – I presume your approach was to put in 6×6 elements – so it’s just to mitigate eventual discrepancies

solved Getting TypeError: ‘int’ object is not subscriptable [closed]