0

I'm doing a program in Python that multiplies two matrices of the dimension that the user enters. The problem I have is that the user must enter the values ​​for each line in the input and my program can only obtain a value for each input. I've tried using .split () but when it does the multiplication sends me this error:

TypeError : can not multiply sequence by non -int of type 'list'.

My code is:

matriza=[]
matrizb=[]
matrizc=[]
orden=int(input("Ingresa el orden de las matrices: "))

#Para obtener la primer matriz
for i in range(0,orden):
    matriza.append([0]*orden)
for j in range(0,orden):
    matrizb.append([0]*orden)
for k in range(0,orden):
    matrizc.append([0]*orden)
for i in range(0,orden):
    for j in range(0,orden):
        matriza[i][j]=int(input("entrada renglon para la primer         matriz"))

print "La primer matriz que introdujiste fue:" "\n" ,matriza, "\n"

#Para obtener la segunda matriz
for i in range(0,orden):
    for j in range(0,orden):
        matrizb[i][j]=int(input("entrada renglon para la segunda matriz"))
print "La segunda matriz que introdujiste fue:" "\n" ,matrizb, "\n"

#Para la multiplicación de las dos matrices
for i in range(0,orden):
    for j in range(0,orden):
        for k in range(0,orden):

            matrizc[i][j]+=matriza[i][k]*matrizb[k][j]
print "La matriz que resulta de multiplicar las matrices que   introdujiste es:" "\n" ,matrizc
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Elias
  • 1

2 Answers2

1

First, since you're on Python 2 (judging by the print syntax), you should be using raw_input instead of input.

If you want the user to supply all values of a row on one line of input, you should use [int(x) for x in raw_input().split()]. You probably assigned the split result to a matrix cell instead of using its elements to populate an entire row when you were having problems earlier:

matriza=[]
matrizb=[]
matrizc=[]
orden=int(input("Ingresa el orden de las matrices: "))

for i in range(orden):
    matriza.append([int(x) for x in raw_input("Enter a row of matrix A:").split()])
...
user2357112
  • 260,549
  • 28
  • 431
  • 505
-1

In Python you can accept lists directly as input. Ex:

matrixA = input("Please enter Matrix A")

The user can enter something like this :

[[1,2],[3,4]]

Now you can directly proceed with your calculations. Coming to your approach in your program, if you were using split, you are required to convert your input to int. Split creates an array of str objects. str cannot be multiplied and hence you encountered TypeError. Although Python is intelligent and can auto cast variables in many cases it cannot do so always and we need to be vary about it.

  • 2
    Using Python 2's `input` function is a really bad habit to get into, because a malicious user can put in something like `__import__('os').system("whatever")` and install a keylogger or hold your files for ransom or all sorts of other nasty things. – user2357112 May 06 '16 at 16:58
  • I agree with @user2357112: security flaws like he or she just demonstrated are the reason Python 2's `input` was completely removed from Python 3 (which renamed the old `raw_input` to `input`). I think the warning in [Python 2's description of `input`](https://docs.python.org/2/library/functions.html#input) is far too subtle. It merely says "Consider using the `raw_input()` function...", without the big, red box that surrounds security flaws mentioned in other Python docs. – Kevin J. Chase May 06 '16 at 17:14