Python Forum
Adding graph points and formating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding graph points and formating
#1
Hi,

I'm creating a code to input 2 points, (x,y) from the user. I've done that, and now I'd like to add these. However, I want to display the points which were input first. I know there's a problem in these lines:

    def get_nums(self):
        return "{[0],[1]}".format(self.x1, self.y1)
What I'd like to do is something like this, but it won't work:

    def get_nums(self):
        return "point 1 = ", "{[0],[1]}".format(self.x1, self.y1)
Here's my full code:
class Input_Points:

    # constructor, arguement receives 4 instance attributes
    def __init__(self,x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2

    """Get integers from user"""
    def set_nums(self):
        self.x1 = int(input('enter x1: '))
        self.y1 = int(input('enter y1: '))
        self.x2 = int(input('enter x2: '))
        self.y2 = int(input('enter y2: '))

    def get_nums(self):
        return "{[0],[1]}".format(self.x1, self.y1)

# Class: Input_Points
w1 = Input_Points("","","","") # no default parameters
w1.set_nums()
w1.get_nums()
When I run the code, it takes the points, but then returns this error:

Quote: return "{[0],[1]}".format(self.x1, self.y1)
TypeError: 'int' object is not subscriptable

Any ideas where I'm going wrong?
Reply
#2
I think that what you want is to change your return statement to this:
 return "{}, {}".format(self.x1, self.y1)
Reply
#3
(Jan-17-2021, 04:28 PM)BashBedlam Wrote: I think that what you want is to change your return statement to this:
 return "{}, {}".format(self.x1, self.y1)

Thanks! I extended on this to make it print, and it worked with this code:

# format the results, using string formatting
    def get_nums(self):
        return print("point 1 = ({},{})".format(self.x1, self.y1))
What I'd like to do now is cycle through the points, and have the point I'm on, e.g. point 2, print out. I've tried this code, but doesn't seem to work:
    def get_nums(self, increment):
        while (increment < 3):
            #print("point", increment, "= ({},{})".format(self.x1, self.y1))
            print("point", increment, "= ({},{})".format(self.x[increment], self.y[increment]))
            increment += 1
If I only use the top line w/the comment, it will increment as expected. However, if I instead only try to use the code as is, I get this error:

Quote:print("point", increment, "= ({},{})".format(self.x[increment], self.y[increment]))
AttributeError: 'Input_Points' object has no attribute 'x'

Here's my full code:
class Input_Points:

    __hiddenVar = 1 #hidden variable, to be used only within this class for increments


    # constructor, arguement receives 4 instance attributes
    #def __init__(self,x1, y1, x2, y2, __hiddenVar):
    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        #self.__hiddenVar = __hiddenVar + 1

    """Get integers from user"""
    def set_nums(self):
        self.x1 = int(input('enter x1: '))
        self.y1 = int(input('enter y1: '))
        self.x2 = int(input('enter x2: '))
        self.y2 = int(input('enter y2: '))

    """
     #code below works
        # format the results, using string formatting
    def get_nums(self):
        return print("point 1 = ({},{})".format(self.x1, self.y1))
    """



    # format the results, using string formatting
    def get_nums(self, increment):
        while (increment < 3):
            #print("point", increment, "= ({},{})".format(self.x1, self.y1))
            print("point", increment, "= ({},{})".format(self.x[increment], self.y[increment]))
            increment += 1

        #return print("point = ", increment, "{},{}".format(self.x1, self.y1))

# Class: Input_Points
w1 = Input_Points("","","","") # no default parameters
w1.set_nums()
#w1.get_nums()

w1.get_nums(w1._Input_Points__hiddenVar)
Reply
#4
FYI x1 is not the same as x[1] or x[increment]. x1 is a variable name and x[1] is a reference to a list element. I've added two lists to the get_nums () method so that you can reference your variables as list elements.
Personally, I would reconsider the use your class variable --hiddenVar.

class Input_Points:
 
    __hiddenVar = 0 #hidden variable, to be used only within this class for increments
 
 
    # constructor, arguement receives 4 instance attributes
    #def __init__(self,x1, y1, x2, y2, __hiddenVar):
    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        # self.__hiddenVar = __hiddenVar
 
    """Get integers from user"""
    def set_nums(self):
        self.x1 = int(input('enter x1: '))
        self.y1 = int(input('enter y1: '))
        self.x2 = int(input('enter x2: '))
        self.y2 = int(input('enter y2: '))
 
    """
     #code below works
        # format the results, using string formatting
    def get_nums(self):
        return print("point 1 = ({},{})".format(self.x1, self.y1))
    """
 
 
 
    # format the results, using string formatting
    def get_nums(self, increment):
        x = [self.x1, self.x2]
        y = [self.y1, self.y2]
        while (increment < 2):
            #print("point", increment, "= ({},{})".format(self.x1, self.y1))
            print("point", increment, "= ({},{})".format(x[increment], y[increment]))
            increment += 1
 
        #return print("point = ", increment, "{},{}".format(self.x1, self.y1))
 
# Class: Input_Points
w1 = Input_Points("","","","") # no default parameters
w1.set_nums()
#w1.get_nums()
 
w1.get_nums(w1._Input_Points__hiddenVar)
Reply
#5
That was quite helpful, thanks! I got it to work, and now trying to add points together. I created a new method, def add_points. It seems to "almost" work, but my methodcall, w1.add_points(w1.get_nums()) gives the error:
Quote: x_f = x[0][0] + x[0][1] # x1 + x2
NameError: name 'x' is not defined

What I really wanted to do was pass the entire methodget_nums() into def add_points but I can't seem to get it to work.

I also tried instead using the methoddef add_points(*points): #unpacking 2x2 tuple with the function call: w1.add_points(points) to unpack the 2x2 tuple in the add_points method, but it doesn't work either.

I also tried the method def sum_points(self) which used the attributes self.x and self.y (which were already set to contain the addition of the coordinates), but that didn't work either.

Any ideas?

class Input_Points:

    # constructor, arguement receives 4 instance attributes
    def __init__(self, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2

        #test ideas below
        self.x = x1 + x2
        self.y = y1 + y2

    """Get integers from user"""
    def set_nums(self):
        self.x1 = int(input('enter x1: '))
        self.y1 = int(input('enter y1: '))
        self.x2 = int(input('enter x2: '))
        self.y2 = int(input('enter y2: '))

    # format the results, using string formatting
    def get_nums(self):
        # tuples for x and y coordinates
        x = [self.x1, self.x2]
        y = [self.y1, self.y2]

        increment = 0
        while (increment < 2):
            #print display is different than how coordinates are stored. Below is not in a tuple, just display
            print("point", increment + 1, "= ({},{})".format(x[increment], y[increment]))
            increment += 1

        return x,y # returned in two tuples. Note that while points are printed, tuple return contain x-coordinates, same for y


    #def add_points(*points):  #unpacking 2x2 tuple
    def add_points(self, get_nums):

            # use with: def add_points(self, get_nums):
            # get final points, using when *points is passed into function
            x_f = x[0][0] + x[0][1]  # x1 + x2
            y_f = y[0][0] + y[0][1]  # y1 + y2

            """
            # use with: def add_points(*points):  #unpacking 2x2 tuple
            # get final points, using when *points is passed into function
            x_f = points[0][0] + points[0][1]  # x1 + x2
            y_f = points[0][0] + points[0][1]  # y1 + y2
            """

            print("x_f =",x_f)
            print("y_f =", y_f)

            return x_f, y_f

    #below doesn't work, as blanks are printed for self.x and self.y
    """    
    def sum_points(self):
        print("x_final = ", self.x)
        print("y_final = ", self.y)
        #return self.x,self.y
    """

# Class: Input_Points
w1 = Input_Points("", "", "", "")  # no default parameters
w1.set_nums()
points = w1.get_nums() # returns a 2X2 tuple, with x-coordinates in row 0, y in row 1
w1.add_points(w1.get_nums())

"""
x_points = points[0][0],points[0][1] #puts x1, x2 into x-tuple
y_points = points[1][0],points[1][1] #puts y1, y2 into y-tuple
print("x_points =",x_points)
print("y_points =",y_points)
"""
#print(points[1][0]) # display y1 as test, row 1, column 0 - y1


#w1.add_points(points)  # passes the 2x2 tuple, 'points' into function
#w1.add_points(x_points,y_points)

#w1.sum_points()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formating generated .data file to XML malcoverc 3 1,315 Apr-14-2022, 09:41 PM
Last Post: malcoverc
  creating two points (x,y) and adding them together.. azhag 2 2,992 Nov-20-2021, 07:22 AM
Last Post: ghoul
  propper formating paracelsusx 2 1,861 Jul-16-2021, 09:17 AM
Last Post: perfringo
  pylab, labeling points on a graph. Dasiey12 0 1,670 Apr-04-2021, 01:08 AM
Last Post: Dasiey12
  Adding markers to Folium map only adding last element. tantony 0 2,094 Oct-16-2019, 03:28 PM
Last Post: tantony
  Issue with ginput and adding points to an exisiting plot gaminer 0 1,590 Oct-12-2019, 07:08 PM
Last Post: gaminer
  tuple and formating problem darktitan 7 3,324 Feb-17-2019, 07:37 PM
Last Post: marienbad
  finding angle between three points on a 2d graph qrani 4 13,970 Nov-20-2018, 06:10 AM
Last Post: Gribouillis
  pymysql: formating ouput of query wardancer84 18 8,176 Oct-04-2018, 01:54 PM
Last Post: wardancer84
  output formating mcmxl22 2 3,044 Jun-19-2017, 07:21 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020