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:

Calculate midpoints of a line

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:

Flowchart: Calculate midpoints of a line.

Python Code Editor:

 

Previous: Write a Python program to get the details of math module.
Next: Write a Python program to hash a word.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.