-2

I'm making a point class in python and I am encountering an error with my makeVectorTo method (a method that takes a second Point as input, and returns a new Vector that goes from starting Point to the second Point). When I try to print it, I am getting this error: AttributeError: 'tuple' object has no attribute 'h'.

How do I get rid of this and print it the way I want?

Here's my code:

class Point:
    def __init__(self, x, y):
        self.x = float(x)
        self.y = float(y)

    def makeVectorTo(self, coor):
      dx = abs(self.x - coor.x)
      dy = abs(self.y - coor.y)
      newCoor = (dx, dy)
      return newCoor

###Testing Point Class###
vec1 = point1.makeVectorTo(point2)
print(vec1.h, vec1.v, "should be 0,3")

The error looked like this:

Traceback (most recent call last):
  File "main.py", line 30, in <module>
    print(vec1.h, vec1.v, "should be 0,3")
AttributeError: 'tuple' object has no attribute 'h'
  • 1
    What part of this code were you expecting to make a Vector? – user2357112 Mar 18 '22 at 16:12
  • 2
    At no point anywhere in your code do you declare an instance variable called `h` or `v`. – Silvio Mayolo Mar 18 '22 at 16:13
  • 1
    `makeVectorTo` returns a tuple. You can index into a tuple, but the error is self explanatory: A tuple doesn't have a `.h` or `.v` attribute you can access – G. Anderson Mar 18 '22 at 16:13
  • 1
    nowhere in your code do you define what `vec1.h`, `vec1.v` are – konstanze Mar 18 '22 at 16:13
  • 1
    if this is an assignment, they may be expecting you to implement some `Vector` class which has v, h, and position members and return it from `makeVectorTo()` [(which should practically be "snake cased" `make_vector_to()`)](https://stackoverflow.com/a/159745/4541045) – ti7 Mar 18 '22 at 16:15

2 Answers2

0

You are returning a tuple, so you should call by index your 'newCoor'

class Point:
    def __init__(self, x, y):
        self.x = float(x)
        self.y = float(y)

    def makeVectorTo(self, coor):
      dx = abs(self.x - coor.x)
      dy = abs(self.y - coor.y)
      newCoor = (dx, dy)
      return newCoor


point1=Point(1,1)
point2=Point(2,2)


###Testing Point Class###
vec1 = point1.makeVectorTo(point2)
# print(vec1.h, vec1.v, "should be 0,3")
print(vec1[0], vec1[1], "should be 0,3")
0

As I understand your code, you new to define your Vector Class too

class Point:
    def __init__(self, x, y):
        self.x = float(x)
        self.y = float(y)

    def makeVectorTo(self, coor):
      dx = abs(self.x - coor.x)
      dy = abs(self.y - coor.y)
      return Vector(dx, dy) # Note: calling Vector class


# Note: New Vector class
class Vector:
    def __init__(self, dx, dy):
        self.h = dx
        self.v = dy


# Testing
point1 = Point(0, 0)
point2 = Point(0, 3)
vec1 = point1.makeVectorTo(point2)
print(vec1.h, vec1.v, "should be 0.0,3.0") # Note: expect floats here not int

Note: abs only remove -tive sign for a int. If you want to convert a float into proper number then use int or round

sam
  • 1,819
  • 1
  • 18
  • 30