﻿ Python: Test AB and CD are orthogonal or not - w3resource # Python: Test AB and CD are orthogonal or not

## Python Basic - 1: Exercise-55 with Solution

There are four different points on a plane, P(xp,yp), Q(xq, yq), R(xr, yr) and S(xs, ys). Write a Python program to test AB and CD are orthogonal or not.

Input:
xp,yp, xq, yq, xr, yr, xs and ys are -100 to 100 respectively and each value can be up to 5 digits after the decimal point It is given as a real number including the number of.
Output:
Output AB and CD are not orthogonal! or AB and CD are orthogonal!.

Sample Solution:

Python Code:

``````while True:
try:
print("Input xp, yp, xq, yq, xr, yr, xs, ys:")
x_p, y_p, x_q, y_q, x_r, y_r, x_s, y_s = map(float, input().split())
pq_x, pq_y = x_q - x_p, y_q - y_p
rs_x, rs_y = x_s - x_r, y_s - y_r
rs = pq_x*rs_x + pq_y*rs_y
if abs(rs) > 1e-10:
print("AB and CD are not orthogonal!")
else:
print("AB and CD are orthogonal!")
except:
break
``````

Sample Output:

```Input xp, yp, xq, yq, xr, yr, xs, ys:
4.5 -2.5 -2.5 4.5 3.5 3.5 3.8 -3.5
AB and CD are not orthogonal!
```

Flowchart: Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz

﻿

## Python: Tips of the Day

Python: Use Enumerate() In for Loops

```>>> students = ('John', 'Mary',  'Mike')
>>> for i, student in enumerate(students):
...     print(f'Iteration:  {i}, Student: {student}')
...
Iteration: 0, Student: John
Iteration: 1, Student: Mary
Iteration: 2, Student: Mike
>>> for i, student in enumerate(students,  35001):
...      print(f'Student Name: {student}, Student ID #: {i}')
...
Student Name: John, Student ID #: 35001
Student Name: Mary, Student ID #: 35002
Student Name: Mike, Student ID #: 35003
```