Class错误'str'对象没有属性
Class error 'str' object has no attribute
我的代码出现以下错误:
Enter your first 3x3 matrix:
matrices.matrix_one("")
File "C:\Users\ablev\eclipse-workspace\SDEV300Lab4\matrix.py", line 61, in matrix_one
self.MATRIX1=[]
AttributeError: 'str' object has no attribute 'MATRIX1'
程序应该要求用户输入两个矩阵,然后计算这两个矩阵的相加。我是 python 的新手,可能 class 的结构有误。任何帮助表示赞赏。谢谢
class matrices:
def __init__(self,MATRIX1,MATRIX2):
self.MATRIX1 = ''
self.MATRIX2 = ''
def matrix_one(self):
"""first matrix"""
print("Enter your first 3x3 matrix: ")
self.MATRIX1=[]
for i in range(3):
while True:
row=input().split()
row=list(map(int,row))
if len(row) != 3:
print("Please enter 3 rows of 3 columns of\
numbers separated by a space: ")
else:
break
self.MATRIX1.append(row)
print("Your first 3x3 matrix is: ")
for i in range(3):
for j in range(3):
print(self.MATRIX1[i][j],end=" ")
print()
def matrix_two(self):
"""asking user for input of second matrix"""
print("Enter your second 3x3 matrix: ")
self.MATRIX2=[]
for i in range(3):
while True:
row=input().split()
row=list(map(int,row))
if len(row) != 3:
print("Please enter 3 rows of 3 columns of\
numbers separated by a space: ")
else:
break
self.MATRIX2.append(row)
print("Your second 3x3 matrix is: ")
for i in range(3):
for j in range(3):
print(self.MATRIX2[i][j],end=" ")
print()
def add(self):
'''function to subtract results of matrix'''
results = np.add(self.MATRIX1,self.MATRIX2)
return results
matrices.matrix_one("")
matrices.matrix_two("")
matrices.add("")
通过调用 matrices.matrix_one("")
,您将空字符串作为 self
传递给 matrix_one
。
您可能想创建一个 class 的实例,以便在调用方法时 self
引用 class 实例:
m = matrices()
m.matrix_one()
m.matrix_two()
m.add()
我的代码出现以下错误:
Enter your first 3x3 matrix:
matrices.matrix_one("")
File "C:\Users\ablev\eclipse-workspace\SDEV300Lab4\matrix.py", line 61, in matrix_one
self.MATRIX1=[]
AttributeError: 'str' object has no attribute 'MATRIX1'
程序应该要求用户输入两个矩阵,然后计算这两个矩阵的相加。我是 python 的新手,可能 class 的结构有误。任何帮助表示赞赏。谢谢
class matrices:
def __init__(self,MATRIX1,MATRIX2):
self.MATRIX1 = ''
self.MATRIX2 = ''
def matrix_one(self):
"""first matrix"""
print("Enter your first 3x3 matrix: ")
self.MATRIX1=[]
for i in range(3):
while True:
row=input().split()
row=list(map(int,row))
if len(row) != 3:
print("Please enter 3 rows of 3 columns of\
numbers separated by a space: ")
else:
break
self.MATRIX1.append(row)
print("Your first 3x3 matrix is: ")
for i in range(3):
for j in range(3):
print(self.MATRIX1[i][j],end=" ")
print()
def matrix_two(self):
"""asking user for input of second matrix"""
print("Enter your second 3x3 matrix: ")
self.MATRIX2=[]
for i in range(3):
while True:
row=input().split()
row=list(map(int,row))
if len(row) != 3:
print("Please enter 3 rows of 3 columns of\
numbers separated by a space: ")
else:
break
self.MATRIX2.append(row)
print("Your second 3x3 matrix is: ")
for i in range(3):
for j in range(3):
print(self.MATRIX2[i][j],end=" ")
print()
def add(self):
'''function to subtract results of matrix'''
results = np.add(self.MATRIX1,self.MATRIX2)
return results
matrices.matrix_one("")
matrices.matrix_two("")
matrices.add("")
通过调用 matrices.matrix_one("")
,您将空字符串作为 self
传递给 matrix_one
。
您可能想创建一个 class 的实例,以便在调用方法时 self
引用 class 实例:
m = matrices()
m.matrix_one()
m.matrix_two()
m.add()