def datos_matriz ():
print('Cuantos renglones quieres?')
renglones=int(input())
print('Cuantas columnas quieres?')
columnas=int(input())
matriz = []
for ren in range(renglones):
lista=[]
for col in range(columnas):
print('Ingresa el dato para el renglon ',ren, 'y la columna ',col)
dato=int(input())
lista.append(dato)
matriz.append(lista)
return matriz
def mostrar_matriz(matriz):
renglones=len(matriz)
columnas=(len(matriz[0]))
for ren in range(renglones):
for col in range(columnas):
print(matriz[ren][col], end =' ')
print()
def main():
mat=datos_matriz()
mostrar_matriz(mat)
Write a function that receives an array as a parameter, given by the user in the main() function, and that returns the sum of all the elements of the array
I have this code to take an array based on user data and display the array.
You have practically done the function of adding the entire matrix, and it is very similar to
mostrar_matriz(matriz)
, but instead of printing each element, you accumulate it in a sum variable and return it at the end.The above would solve your problem, however I saw in the edits that you also require the sum of
x
row andy
column, in that case, you can build those 2 functions and use either one to sum the entire matrix.And the main function would be: