[Tutor] line class

Christopher Spears cspears2002 at yahoo.com
Tue Jul 8 06:52:28 CEST 2008

For problem 13-6 out of Core Python Programming, I created a line class that consists of two points.  The line class has the following methods: __repr__, length, and slope.  Here is the code:


import sys,math

class Point(object):
    def __init__(self, x=0.0,y=0.0):
        self.x = float(x)
	self.y = float(y)
    def __repr__(self):
        coord = (self.x,self.y)
	return coord
    def __str__(self):
	point_str = "(%f,%f)" % (self.x, self.y)
	return point_str
class Line(object):
    def __init__(self, p1, p2):
        self.p1 = Point(x1,y1)
	self.p2 = Point(x2,y2)
    def __str__(self):
        x1,y1 = self.p1.x,self.p1.y
	x2,y2 = self.p2.x,self.p2.y
	line = "((%f,%f),(%f,%f))" % (x1,y1,x2,y2)
	return line
    __repr__ = __str__
    def length(self):
        dist_x = abs(self.p2.x - self.p1.x)
	dist_y = abs(self.p2.y - self.p1.y)
        dist_x_squared = dist_x ** 2
        dist_y_squared = dist_y ** 2
	line_length = math.sqrt(dist_x_squared + dist_y_squared)
        return line_length
    def slope(self):
        dist_y = self.p2.y - self.p1.y
	dist_x = self.p2.x - self.p1.x
	line_slope = dist_y/dist_x
	return line_slope
if __name__ == '__main__':
    print "Creating a Line"
    x1 = raw_input("Enter a x1 value: ")
    y1 = raw_input("Enter a y1 value: ")
    p1 = Point(x1,y1)
    #print p1
    x2 = raw_input("Enter a x2 value: ")
    y2 = raw_input("Enter a y2 value: ")
    p2 = Point(x2,y2)
    #print p2
    line = Line(p1,p2)
    print "What are the lines attributes?"
    print "Select one:"
    print "1) Display line"
    print "2) Display line's length"
    print "3) Display line's slope"
    print "4) Quit program"
    choice_string = raw_input("Make a choice: ")
        choice = int(choice_string)
    except ValueError:
        sys.exit("Not an integer!  Goodbye!")
    if choice == 1:
        print line
    elif choice == 2:
        line_length = line.length()
        print "Length is %f " % line_length
    elif choice == 3:
        line_slope = line.slope()
        print "Slope is %f " % line_slope
    elif choice == 4:
        print "Goodbye!"
        sys.exit("Wrong response Goodbye!")

For the most part, my script works fairly well except under the following circumstances:

Creating a Line
Enter a x1 value: 0
Enter a y1 value: 0
Enter a x2 value: 0
Enter a y2 value: 1
What are the lines attributes?
Select one:
1) Display line
2) Display line's length
3) Display line's slope
4) Quit program
Make a choice: 3
Traceback (most recent call last):
  File "line.py", line 79, in ?
    line_slope = line.slope()
  File "line.py", line 42, in slope
    line_slope = dist_y/dist_x
ZeroDivisionError: float division

Basically, if the two the x values are the same, I will get a ZeroDivisionError.  A line in this case would simply point straight up.  What would slope be in this case?  I will admit that this is probably a math problem not a programming one, but I decided to run it by you anyway.



More information about the Tutor mailing list