-1

I do not get any output when I run the entire script.

class Vectors():

    def __init__(self, coordinates):
        try:
            if not coordinates:
                raise ValueError
            self.coordinates = tuple(coordinates)
            self.dimension = len(coordinates)

        except ValueError:
            print('The vector cannot be non-empty.')
        except TypeError:
            print('The object type is not iterable.')

    def magnitude(self):

        lst = [x**2 for x in self.coordinates]
        return math.sqrt(sum(lst))


    def dot_product(self, vector):
        lst = [round(x*y, 3) for x,y in zip(self.coordinates, vector.coordinates)]
        return sum(lst)

vector_v1 = Vectors([7.887, 4.138]) 
vector_w1 = Vectors([-8.802, 6.776])

vector_v1.dot_product(vector_w1)

vector_v1.magnitude()
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Not clear why you expect output, but no output is not necessarily an error – OneCricketeer Apr 07 '18 at 05:14
  • This is kind of the opposite problem of the dups, but the answers there will explain things. – abarnert Apr 07 '18 at 05:15
  • The key is that to get any output, you have to `print` something somewhere. You can just change the last two lines to `print(vector_v1.do_product(vector_v2))` and `print(vector_v1.magnitude())`. Or you can change the `return` statements in the function to `print` calls. But without one of those two, you're doing the work, returning a value, and then just ignoring that value and moving on, without ever printing anything. – abarnert Apr 07 '18 at 05:16

1 Answers1

0

I think that you are expecting to see the results of your dot product and magnitude functions.

If this is the case, you don't get output of these functions since they only return something to the caller of that function, and the caller of the function doesn't do something with it (printing it to the console).

If you want to let the caller show on the console what the result (returned value) of the function call was, then you should get the returned value and print it.

vector_v1 = Vectors([7.887, 4.138])
vector_w1 = Vectors([-8.802, 6.776])
v1_w1_dot = vector_v1.dot_product(vector_w1)
print(v1_w1_dot)
v1_magnitude = vector_v1.magnitude()
print(v1_magnitude)

Since you don't seem to use the calculated dot product or manitude further in the code, you could also just write it shorter:

vector_v1 = Vectors([7.887, 4.138])
vector_w1 = Vectors([-8.802, 6.776])
print(vector_v1.dot_product(vector_w1))
print(vector_v1.magnitude())
Sander Vanden Hautte
  • 2,138
  • 3
  • 22
  • 36