The task: to find the string of the array with the smallest value of the sum of all its elements and display the value of the sum and the row number

a = [] j = 5 k = 2 s = 0 m = 100000 for i in range(j): for p in range(k): a.append(int(input())) for i in range(j): for p in range(k): s = s + int(a[i][p]) if s <= m: m = s b = i print(b, m) 

    1 answer 1

    a.append(int(input())) - adds an integer to the array, the index of the new element i * j + p .

    a[i] - gets an integer from the array.

    a[i][p] - tries to take an element at index p from an integer. Can not.

    You probably wanted to:

     for i in range(j): for p in range(k): s = s + int(a[i * j + p]) 

    Or

     for i in range(j): a.append([]) for p in range(k): a[i].append(int(input())) for i in range(j): for p in range(k): s = s + int(a[i][p]) 
    • As an option, store the elements of the array as strings, and iterate over the characters, translating to store the result in a temporary variable. - Vladimir Yaremenko
    • Tobish s = 0 for symb in a[i]: s+=int(symb) provided that a.append (input ()) - Vladimir Yaremenko
    • one
      Question: in the sense of - the problem of a two-dimensional array (rows, columns), and in fact - you are working with a clearly one-dimensional structure and try to interpret it yourself through a complex recalculation of indices. Is it so conceived? In other words, is this a learning task so cleverly formulated or is this how you really want to work with arrays? - passant