Search code examples
pythonmatrixraw-input

Multiplying matrices in python, error in raw_input's


I would like to create a program that multiplies two matrices given by the user. I want the user to enter the rows of the 1st matrix, and then I want to save each row in a dictionary, with the dictionary keys being the number of the row. Nevertheless, when I make raw_input to ask the user the ith row, I get the error:

TypeError: cannot concatenate 'str' and 'int' objects

This is my code:

print "this program computes the product of two square matrices with real entries"
n = raw_input("Enter number of columns=Number of rows")
rowsofmatrix1={}
columnsofmatrix2={}
for i in range (1,n+1):
   rowsofmatrix1[i]=raw_input("Enter row number"+str(i)+"of the first matrix as a list")
for j in range (1,n+1):
   columnsofmatrix2[j]=raw_input("Enter column number"+str(j)+"of the second matrix as a list")
print rowsofmatrix1

Solution

  • You need to convert n in order to use it in the range function. Try to change to the code below:

    n = int(raw_input("Enter number of columns=Number of rows"))