117k views
2 votes
Write a Python3 program to check if 3 user entered points on the coordinate plane creates a triangle or not. Your program needs to repeat until the user decides to quit, and needs to deal with invalid inputs.

1 Answer

1 vote

Answer:

tryagain = "Y"

while tryagain.upper() == "Y":

x1 = int(input("x1: "))

y1 = int(input("y1: "))

x2 = int(input("x2: "))

y2 = int(input("y2: "))

x3 = int(input("x3: "))

y3 = int(input("y3: "))

area = abs(x1 *(y2 - y3) + x2 * (y1 - y3) + x3 * (y1 - y2))

if area > 0:

print("Inputs form a triangle")

else:

print("Inputs do not form a triangle")

tryagain = input("Press Y/y to try again: ")

Step-by-step explanation:

To do this we simply calculate the area of the triangle given that inputs are on plane coordinates i.e. (x,y).

If the area is greater than 0, then it's a triangle

If otherwise, then it's not a triangle.

This line initializes iterating variable tryagain to Y

tryagain = "Y"

while tryagain.upper() == "Y":

The following lines get the coordinates of the triangle

x1 = int(input("x1: "))

y1 = int(input("y1: "))

x2 = int(input("x2: "))

y2 = int(input("y2: "))

x3 = int(input("x3: "))

y3 = int(input("y3: "))

This calculates the area

area = abs(x1 *(y2 - y3) + x2 * (y1 - y3) + x3 * (y1 - y2))

This checks for the condition stated above.

if area > 0:

print("Inputs form a triangle") This is printed, if true

else:

print("Inputs do not form a triangle") This is printed, if otherwise

tryagain = input("Press Y/y to try again: ") This prompts the user to try again with another set of inputs

User Arpo
by
6.8k points