H
H
Hikikomori912015-11-01 01:06:06
Python
Hikikomori91, 2015-11-01 01:06:06

Why does the code throw an AttributeError?

class Vector(object):
    def __init__(self, points):
        self.points = points

    def add(self, victor):
        self.victor = victor
        if len(victor.points) == len(self.points):
            return [x + y for x, y in zip(self.points, victor.points)]
        else:
            raise IndexError("different Length")

    def subtract(self, victor1):
        self.victor = victor1
        if len(victor1.points) == len(self.points):
            return [x - y for x, y in zip(self.points, victor1.points)]
        else:
            raise IndexError("different Length")

    def norm(self):
        return [x**2 for x in self.points]

    def dot(self, victor2):
        self.victor = victor2
        if len(victor2.points) == len(self.points):
            return [x * y for x, y in zip(self.points, victor2.points)]
        else:
            raise IndexError("different Length")

a = Vector([1,2])
b = Vector([3,4])
print(a.dot(b))
print(b.add(a))

Error Traceback
:
in
AttributeError: 'list' object has no attribute 'equals'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
throughtheether, 2015-11-01
@Hikikomori91

But where it should work (a site with puzzles), it does not work. How can I rewrite this code to get rid of this unknown genesis error?

There is a suspicion that checking the results in the test environment is implemented by calling the .equals() method. So do this:
1) implement the Vector.equals(self,other) method in your class (is the vector equal to another vector)
2) in the methods Vector.add(...), Vector.subtract(...) return not a list , and a vector (an instance of the Vector class)
3) override the Vector.dot(...) and Vector.norm(...) methods. If they are supposed to implement the dot product and the norm, then why do they return lists and not numbers?
And what is the meaning of these lines, I still don’t understand: self.victor = victoryou don’t seem to use self.victor anywhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question