Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6659499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:02:31+00:00 2026-05-26T02:02:31+00:00

One of my exercises says to write an add method for Points that works

  • 0

One of my exercises says to write an add method for Points that works with either a Point object or a tuple:

  • If the second operand is a Point, the method should return a new Point whose x coordinate is the sum of the x coordinates of the operands, and likewise for the y coordinates.
  • If the second operand is a tuple, the method should add the first element of the tuple to the x coordinate and the second element to the y coordinate, and return a new Point with the result.

This how far I got and I’m not sure if the tuple portion of my code is accurate. Can someone shed some light how I would call this program for the tuple portion. I think I nailed the first part.

Here is my code:

Class Point():
    def__add__(self,other):
            if isinstance(other,Point):
                    return self.add_point(other)
            else:
                    return self.print_point(other)

    def add_point(self,other):
            totalx = self.x + other.x
            totaly = self.y + other.y
            total = ('%d, %d') % (totalx, totaly)
            return total

    def print_point(self):
            print ('%d, %d) % (self.x, self.y)

    blank = Point()
    blank.x = 3
    blank.y = 5
    blank1 = Point()
    blank1.x = 5
    blank1.y = 6

That’s what I’ve built so far and I’m not sure how to actually run this with the tuple part. I know if it did blank + blank1 the if portion would run and call the add_point function but how do I initiate the tuple. I’m not sure if I wrote this correctly… please assist.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:02:32+00:00Added an answer on May 26, 2026 at 2:02 am

    Alternatively, if you want to be able to use point.x and point.y syntax, you could implement the following:

    class Point():
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
        def __add__(self, other): 
            if isinstance(other, Point):
                return Point(self.x + other.x, self.y + other.y)
            elif isinstance(other, tuple):
                return Point(self.x + other[0], self.y + other[1])
            else:
                raise TypeError("unsupported operand type(s) for +: 'Point' and '{0}'".format(type(other)))
    
        def __repr__(self):
            return u'Point ({0}, {1})'.format(self.x, self.y) #Remove the u if you're using Python 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One of the exercises in TC++PL asks: Write a function that either returns a
hello i am reading this python book and one of the exercises says: Write
I'm reading the second edition of K&R book and one of the exercises requires
I am doing some exercises so that I can get through recursive stuff. One
I'm studying Java on my own. One of the exercises is the following, however
I have one single file called Exercises.rb def ask(prompt) print prompt, ' ' $stdout.flush
Can any one suggest good resources and practice exercises for Teradata and Business Objects
I was trying out some exercises of db-class.org videos.There was one problem of finding
I'm following a couple of Pythone exercises and I'm stumped at this one. #
I'm trying to teach myself C++, and one of the traditional new language exercises

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.