2
My code must have two matrices each with 5 elements and the first matrix needs to multiply its elements by 3 whereas the elements of the second matrix need to decrease their elements by 3, but it returns an error TypeError: unsupported operand type(s) for -: 'list' and 'int'
caused by the line mt1, mt2 = (mt1) * 2, (mt2) - 5
, how to fix this?
Entrada:
02 03 04 05 06
10 20 30 40 50
Saída esperada:
06 7
09 17
12 27
15 37
16 47
Meu código:
i = 0 # Linha
j = 0 # Coluna
mt1 = input().split() # Matriz 1
mt2 = input().split() # Matriz 2
mt1, mt2 = (mt1) * 2, (mt2) - 5 # PARTE ERRADA DO CÓDIGO
mt3 = zip(mt1, mt2) # Unindo as matrizes em uma com ZIP
for i, j in mt3: # linha e coluna associadas a terceira matriz
print(i,j) # Exibe linha e coluna
Translation of the errro:
TypeError: tipos de operandos não-suportados para -: 'list' e 'int'
. That is, you cannot subtract a list from an integer or an integer from a list... What you want to do is subtract an integer for each element of the list, that is, you must iterate the list and subtract from each iterated item...– fernandosavio
Yes, each element of the first matrix has to be multiplied by 3 and each element of the second matrix subtracted by 3.
– user141036
It wasn’t a question.. I was trying to explain to you that you can’t subtract a number from a list. You can’t do it
lista - 4
, that doesn’t work, you should go through this list and subtract4
of each item in the list covered. You are not doing this in your code– fernandosavio
What version of your python?
– Jonathan de Toni
Spyder (Python 3.6)
– user141036