Python Programming – John Zelle: Chapter 3 ex. 6

Two points in a plane are specified using the coordinates (x1,y1) and (x2,y2). Write a program that calculates the slope of a line through two (non-vertical) points entered by the user.

Chapter 3 Exercise 6

Python

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #Chapter 3 Exercise 6 #Two points in a plane are specified using the coordinates (x1,y1) and (x2,y2). #Write a program that calculates the slope of a line through #two (non-vertical) points entered by the user. def main(): print("This program calculates the slope of a line through 2 points.") print() ptx1, pty1 = eval(input("Input the first x and y point separated by a comma ")) ptx2, pty2 = eval(input("Input the second x and y point separated by a comma ")) slopey = pty2 - pty1 slopex = ptx2 - ptx1 print("The slope is", slopey, '/', slopex) main() |

Run =

This program calculates the slope of a line through 2 points.

Input the first x and y point separated by a comma 2,3

Input the second x and y point separated by a comma 4,8

The slope is 5 / 2