Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Class Python程序遇到错误后继续运行_Class_Python 3.x_Linear Algebra_Theory - Fatal编程技术网

Class Python程序遇到错误后继续运行

Class Python程序遇到错误后继续运行,class,python-3.x,linear-algebra,theory,Class,Python 3.x,Linear Algebra,Theory,我正在为我的线性代数类创建一个包含向量和矩阵类的程序,但是我在字符串化我的矩阵类以打印它时遇到了麻烦。导致问题的是if语句,如果矩阵中的某个条目不是行中的最后一个条目,则在该条目后添加逗号。奇怪的是,我已经将问题隔离到了我的程序中为手头条目的索引分配变量的部分,但是当我在那之后添加一行时,我打印了该变量,试图找出发生了什么,运行程序打印了变量,然后给出了前一行的错误。代码如下: import copy class vector: def __init__(self, entries):

我正在为我的线性代数类创建一个包含向量和矩阵类的程序,但是我在字符串化我的矩阵类以打印它时遇到了麻烦。导致问题的是if语句,如果矩阵中的某个条目不是行中的最后一个条目,则在该条目后添加逗号。奇怪的是,我已经将问题隔离到了我的程序中为手头条目的索引分配变量的部分,但是当我在那之后添加一行时,我打印了该变量,试图找出发生了什么,运行程序打印了变量,然后给出了前一行的错误。代码如下:

import copy
class vector:

    def __init__(self, entries):
        if type(entries) == list:

            self.elements = []
            self.dimensionality = len(entries)
            for entry in entries:
                self.elements.append(entry)
        if type(entries) == vector:
            self.elements = entries.elements

    def __str__(self):
        buff = "("
        for e in self.elements:
            buff += str(e)
            if self.elements.index(e) < len(self.elements) - 1:
                buff += ", "
        buff += ")"
        return buff
    def __getitem__(self,index):
        return self.elements[index]

    def __len__(self):
        return len(self.elements)

    def __mul__(self, otherVector):
        if self.dimensionality != otherVector.dimensionality:
            raise RuntimeError("Cannot multiply vectors of different dimensions")
        else:
            product = 0
            for e in self.elements:
                product += e * otherVector.elements[self.elements.index(e)]
            return product

    def __eq__(self, otherVariable):
        return size(self) == size(otherVariable)

    def size(x):
        return (x * x)**(1/2)



class matrix:
    def __init__(self, entries):
        for i in entries:
            if len(entries[0]) != len(i):
                raise RuntimeError("All rows of matrix must contain the same number of entries")
        self.elements = []
        for row in entries:
            self.elements.append(vector(row))
    def __str__(self):
        buff = "("
        for row in self.elements:
            buff += str(row)
            a = self.elements.index(row)   #this is the line that prompts the error
            b = len(self.elements) - 1
            print (a)                     #but this line executes before the error cuts off the rest of the program
            print(b)
            print(a<b)
            if a < b :
                buff += ", "
        buff += ")"
        return buff

print(matrix([[1,2],[2,3]]))
导入副本
类向量:
定义初始化(self,条目):
如果类型(条目)=列表:
self.elements=[]
self.dimensionality=len(条目)
对于条目中的条目:
self.elements.append(条目)
如果类型(条目)=向量:
self.elements=entries.elements
定义(自我):
buff=“(”
对于self.elements中的e:
buff+=str(e)
如果self.elements.index(e)打印(a使用
self.save
引用类中定义的函数

错误来自第127行中对
print
的调用,整行没有被执行。除了堆栈跟踪之外,您真的在控制台中看到了打印输出吗

从概念上讲,所讨论的行,
print(矩阵([[1,2],[2,3]])
执行以下操作:

  • 已成功创建
    矩阵
    实例
  • print
    在该
    矩阵
    实例上调用
    \uuu str\uuu
  • 调用
    向量
    实例列表中的
    索引
  • 索引
    需要在列表中查找匹配值,并对列表的每个成员调用
    \uuuuueq\uuuu
    ,以查找匹配项
  • 您原来的
    \uuuuuuueq\uuuuuuu
    代码调用了一个名为
    size
    的缺失函数(您注意到了,并修复了该函数)

我很惊讶,除了错误之外,它产生了任何输出。

你能告诉我具体的吗?我在哪里插入?请考虑编辑你的帖子来添加更多关于你的代码所做的解释以及为什么它会解决这个问题。一个答案几乎只包含代码(即使它正在工作)。通常不会帮助OP了解他们的问题。“0 1真正的回溯(最后一次调用):文件“/Users/sebbole/Documents/vectors.py”,第163行,打印(矩阵([[1,2],[2,3]])文件“/Users/sebbole/Documents/vectors.py”,第100行,str a=self.elements.index(row)文件“/Users/sebbole/Documents/vectors.py”,第47行,在eq返回大小(self)=size(otherVariable)NameError:name'size'未定义“是我运行它时所做的,正如您所看到的,它在错误后运行该行…您的示例中只有一行过程代码(
print(matrix([[1,2],[2,3]])
)。我看到的是那一行产生了一个错误。你看到错误后的哪一行?是的,但那一行正在调用函数,而错误正在函数中发生。我指的是函数中发生的情况。我建议使用
pdb
检查发生的情况。你可以用这一行设置断点
import pdb;pdb.set\trace()
Traceback (most recent call last):
  File "/Users/sebpole/Documents/vectors.py", line 127, in <module>
    print(matrix([[1,2],[2,3]]))
  File "/Users/sebpole/Documents/vectors.py", line 83, in __str__
    a = self.elements.index(row)
  File "/Users/sebpole/Documents/vectors.py", line 38, in __eq__
    return size(self) == size(otherVariable)
NameError: name 'size' is not defined