Reputation: 5
so what I need to do is to tell the user to type values for a matrix and print the center of this one. I've this code but I´m missing that.
import sys
reng = int(input('row: '))
column = int(input('column: '))
matrix = []
suma = 0
lista = []
if reng < 0 or column < 0:
print('NOT VALID')
sys.exit()
else:
for i in range(reng):
matrix.append([0]*column)
for j in range(reng):
for k in range(column):
matrix[j][k] = int(input('enter an integer: '))
print(matrix)
Upvotes: 0
Views: 1281
Reputation: 13195
print(matrix[reng//2][column//2])
will print you the exact middle element of an "odd*odd" matrix. //
is integer division, so for example 3//2
is 1
, which really is the middle one of possible indices 0
, 1
, 2
. Even numbers have a middle-pair, and division produces the higher one (like in case of 4: 4//2
is 2
, which is the "upper middle" element of the possible indices 0
, 1
, 2
, 3
).
Upvotes: 1