# Python Code for Class def\_\_add\_\_ with error

**URL:** <https://forum.codewithmosh.com/t/python-code-for-class-def-add-with-error/21809>\
**Category:** Python\
**Created:** [August 1, 2023, 4:02am UTC](https://forum.codewithmosh.com/t/python-code-for-class-def-add-with-error/21809 "2023-08-01T04:02:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![seantayyebi](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@seantayyebi](https://forum.codewithmosh.com/u/seantayyebi)\
**Post date:** [August 1, 2023, 4:02am UTC](https://forum.codewithmosh.com/t/python-code-for-class-def-add-with-error/21809/1 "2023-08-01T04:02:52Z")

</div>

Getting error message right after def\_\_add\_\_(self, other): any idea anyone thx!!  
def **init** (self, x, y):  
self.x = x  
self.y = y  
def\_\_add\_\_(self, other): _at this point getting error message_  
return Point(self.x + other.x, self.y + other.y)  
point = Point(10, 20)  
other = Point(1. 2)  
combined = point + other  
print(combined.x)

---

<div class="post-metadata">

**Author:** ![Coccobasso](https://avatars.discourse-cdn.com/v4/letter/c/53a042/32.png) [@Coccobasso](https://forum.codewithmosh.com/u/Coccobasso)\
**Post date:** [August 1, 2023, 8:03am UTC](https://forum.codewithmosh.com/t/python-code-for-class-def-add-with-error/21809/2 "2023-08-01T08:03:33Z")

</div>

Hello,

Did you create the class in the first line? before/above the init magic method?

When creating the other-object you have a float within the parenthesis, instead of two ints - (1. 2) instead of (1,2).

When I take your code and try it with a comma instead of a point, and creating a PointClass first it works. Hope this helps you.

class Point():  
def **init** (self, x, y):  
self.x = x  
self.y = y

def **add** (self, other):  
return Point(self.x + other.x, self.y + other.y)

point = Point(10, 20)  
other = Point(1, 2)  
combined = point + other  
print(combined.x)

---

<div class="post-metadata">

**Author:** ![gulshan212](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/gulshan212/32/6217_2.png) [@gulshan212](https://forum.codewithmosh.com/u/gulshan212)\
**Post date:** [August 1, 2023, 3:17pm UTC](https://forum.codewithmosh.com/t/python-code-for-class-def-add-with-error/21809/3 "2023-08-01T15:17:55Z")

</div>

Well, your code have some logical error, you can try below code once and confirm me weather it is working or not ?

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

    def __add__ (self, other):
        return Point(self.x + other.x, self.y + other.y)

point = Point(10, 20)
other = Point(1, 2)
combined = point + other
print(combined.x)

```

Thanks

---

<div class="post-metadata">

**Author:** ![seantayyebi](https://avatars.discourse-cdn.com/v4/letter/s/46a35a/32.png) [@seantayyebi](https://forum.codewithmosh.com/u/seantayyebi)\
**Post date:** [August 2, 2023, 6:10pm UTC](https://forum.codewithmosh.com/t/python-code-for-class-def-add-with-error/21809/4 "2023-08-02T18:10:36Z")

</div>

Worked thx!!

Question Mr. Gulshan(like your name)  
How do i swith Vscode to Intaeractive terminal, shell it is easy doing in IDLE
