I am stuck trying to figure this out. I need to find the slope of a line after the user is prompted to click two points and display the line. I know to find slope you need y2 - y1/x2 - x1 but how can I do that if there are only two points to a line?

from graphics import*

def main():

win = GraphWin("Calculates Slope", 400, 400)
win.setCoords(-100, -100, 100, 100)
win.setBackground("white")

c = Circle(Point(0,0),2)
c.draw(win)
c.setFill("black")

centerPoint = Text(Point(11,8), "(0,0)")
centerPoint.draw(win)

xLine = Line(Point(100,0), Point(-100,0)) 
xLine.draw(win)

yLine = Line(Point(0,-100), Point(0,100))
yLine.draw(win)

for i in range(20):
    tickMark = Text(Point(-10*i,0), "|")
    tickMark.draw(win)

    tickMark = Text(Point(10*i,0), "|")
    tickMark.draw(win)        

for i in range(20):     #start stop step
    tickMark2 = Text(Point(0,-10*i,),"__")  # -10 goes down Y 
    tickMark2.draw(win) # -10*i gives more space

    tickMark2 = Text(Point(0,10*i,),"__")  # 10 goes up Y 
    tickMark2.draw(win)    

point = Text(Point(10,80), "Please click to create the first point")   
point.draw(win)

x1 = win.getMouse()
x1.draw(win)

x2 = win.getMouse()
x2.draw(win)

slope = Text(Point(10,70), "The slope of the line is: ")
slope.draw(win)

main()

Without getting into the code, if you have two points then you have the coordinates (x,y) of both points, therefore you can calculate (y2 - y1)/(x2 - x1). Note the importance of parentheses.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.