python 中的矩阵相乘,raw_input 中的错误

Multiplying matrices in python, error in raw_input's

我想创建一个程序,将用户给定的两个矩阵相乘。我希望用户输入第一个矩阵的行,然后我想将每一行保存在字典中,字典键是行号。然而,当我让 raw_input 询问用户 ith 行时,我得到错误:

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

这是我的代码:

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

您需要转换 n 才能在范围函数中使用它。尝试更改为以下代码:

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