﻿ Python: Calculate midpoints of a line - w3resource

# Python: Calculate midpoints of a line

## Python Basic: Exercise-73 with Solution

Write a Python program to calculate the midpoints of a line.

Pictorial Presentation:

Sample Solution:

Python Code:

``````# Print a message to inform the user that the program will calculate the midpoint of a line.
print('\nCalculate the midpoint of a line :')

# Prompt the user to enter the x and y coordinates of the first endpoint of the line.
x1 = float(input('The value of x (the first endpoint) '))
y1 = float(input('The value of y (the first endpoint) '))

# Prompt the user to enter the x and y coordinates of the second endpoint of the line.
x2 = float(input('The value of x (the first endpoint) '))
y2 = float(input('The value of y (the first endpoint) '))

# Calculate the x-coordinate of the midpoint of the line.
x_m_point = (x1 + x2)/2

# Calculate the y-coordinate of the midpoint of the line.
y_m_point = (y1 + y2)/2
print();

# Print a message to indicate that the program is displaying the midpoint.
print("The midpoint of the line is :")

# Print the x-coordinate of the midpoint.
print( "The midpoint's x value is: ", x_m_point)

# Print the y-coordinate of the midpoint.
print( "The midpoint's y value is: ", y_m_point)
```
```

Sample Output:

```Calculate the midpoint of a line :
The value of x (the first endpoint) 2
The value of y (the first endpoint) 2
The value of x (the first endpoint) 4
The value of y (the first endpoint) 4

The midpoint of line is :
The midpoint's x value is:  3.0
The midpoint's y value is:  3.0
```

Flowchart:

Python Code Editor:

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿