Jan-24-2021, 05:02 PM
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:
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?
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()