-2

I'm a newbie to Python and I came across this question somewhere:

"Produce a programme that can calculate the perimeter of the triangle, the area of the triangle and the height of the triangle from the length of the three sides of a triangle. If the three lengths of the sides of the triangle do not define a valid triangle, a message should be displayed stating that this is not a valid calculation and the process should be terminated."

Anyone know how this could be solved? It's probably easy, but I'm a newbie so yeah

This is what I've got so far:

a = float(input('Please Enter the First side of a Triangle: '))

b = float(input('Please Enter the Second side of a Triangle: '))

c = float(input('Please Enter the Third side of a Triangle: '))

if  a + b >= c and b + c >= a and c + a >= b:

# calculate area and height here


Perimeter = a + b + c

s = (a + b + c) / 2

Area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

print("\n The Perimeter of Triangle = %.2f" %Perimeter);

print(" The Area of a Traiangle is %0.2f" %Area)     
else:
print('Not a valid triangle')

I still have to calculate the height. Everything else seems to be working now :D

DSM
  • 342,061
  • 65
  • 592
  • 494
Anon248
  • 3
  • 3
  • Post the code sample that you tried , so that it will be easy for people to answer. – Strik3r Apr 29 '16 at 09:04
  • Remove the `a, b, c = 1, 1, 1` line and move your three `input()` lines before the if statement to make it work. – Emma Apr 29 '16 at 09:43
  • You can get the height with sin, cos or tan... In python you can access these through the math package... (`import math`) – Emma Apr 29 '16 at 09:51

1 Answers1

0

In mathematics, the triangle inequality states that for any triangle, the sum of the lengths of any two sides must be greater than or equal to the length of the remaining side.

a, b, c = 1, 1, 1 # sides of a triangle
if a + b >= c and b + c >= a and c + a >= b:
    # calculate area and height here
else:
    print('Not a valid triangle')
Emma
  • 430
  • 8
  • 14
  • Thanks, check what I added to my original post – Anon248 Apr 29 '16 at 09:35
  • Is there no easier way to go about calculating the height? – Anon248 Apr 29 '16 at 09:59
  • Not that I know of, maybe you can find a library for that but I really doubt it, the functions are all there if you know how to calculate it in a calculator. Here a list of all math functions: https://docs.python.org/3/library/math.html – Emma Apr 29 '16 at 10:05