Python Forum
Adding graph points and formating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding graph points and formating
#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


Messages In This Thread
RE: Adding graph points and formating - by project_science - Jan-24-2021, 05:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Formating generated .data file to XML malcoverc 3 1,384 Apr-14-2022, 09:41 PM
Last Post: malcoverc
  creating two points (x,y) and adding them together.. azhag 2 3,195 Nov-20-2021, 07:22 AM
Last Post: ghoul
  propper formating paracelsusx 2 1,918 Jul-16-2021, 09:17 AM
Last Post: perfringo
  pylab, labeling points on a graph. Dasiey12 0 1,709 Apr-04-2021, 01:08 AM
Last Post: Dasiey12
  Excel: Apply formating of a sheet(file1) to another sheet(file2) lowermoon 1 2,068 May-26-2020, 07:57 AM
Last Post: buran
  Adding markers to Folium map only adding last element. tantony 0 2,140 Oct-16-2019, 03:28 PM
Last Post: tantony
  Issue with ginput and adding points to an exisiting plot gaminer 0 1,640 Oct-12-2019, 07:08 PM
Last Post: gaminer
  tuple and formating problem darktitan 7 3,464 Feb-17-2019, 07:37 PM
Last Post: marienbad
  finding angle between three points on a 2d graph qrani 4 14,143 Nov-20-2018, 06:10 AM
Last Post: Gribouillis
  pymysql: formating ouput of query wardancer84 18 8,433 Oct-04-2018, 01:54 PM
Last Post: wardancer84

Forum Jump:

User Panel Messages

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