Back

Code Challenge: Line Slope Calculator

Difficulty  

divider

Objective

When graphing lines in Algebra, it is important to understand how to calculate the slope of a line. To calculate slope you need at least two points, and then you apply the Slope Formula:

The slope formula: m equals y-two minus y-one, over x-two minus x-one

where m is the slope, (X1, Y1) is the first point, and (X2, Y2) is the second point.

Skills to Practice

  • Prompting a user for input
  • Saving user input to variables
  • Converting input to the correct data type
  • Performing arithmetic calculations
  • Formatting custom output using variables

Tasks

  • Create a new Python program named cc-line-slope.
  • Prompt the user for four values — the two points (X1, Y1) and (X2, Y2).
  • Calculate the slope using the Slope Formula.
  • Optionally, round the answer to three decimal places.
  • Display the calculated slope in a clear, formatted way.

Sample Output

Sample Output
Enter x1: 2 [Enter]
Enter y1: 3 [Enter]
Enter x2: 12 [Enter]
Enter y2: 14 [Enter]
The slope of the line is 1.1

Commence Challenge