2016-04-29 207 views
-2

我是Python的新手,我碰到这个问题的地方:任何人都知道如何计算三角形的面积/周长/高度?

“生成一个程序,可以计算三角形的周长,三角形的面积和三角形的高度从长度三角形的三条边如果三角形的三条边没有定义有效的三角形,则应显示一条消息,指出这不是一个有效的计算,并且该过程应该终止。

任何人都知道如何解决这个问题?这也许很容易,但我是一个新手,所以是

这是我到目前为止有:

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') 

我还是要计算高度。 D

+0

后,你尝试过,所以,这将是容易让人们回答代码示例。 – San

+0

删除'a,b,c = 1,1,1'行并在if语句之前移动三个'input()'行以使其工作。 – MCManuelLP

+0

你可以用sin,cos或tan来获得高度...在python中你可以通过数学包访问这些...('import math') – MCManuelLP

回答

0

在数学中,triangle inequality指出,对于任何三角形,任何两边的长度总和必须大于或等于剩余边的长度。

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') 
+0

谢谢,请查看我添加到原始文章中的内容 – Anon248

+0

难道没有更容易有关计算身高的方法? – Anon248

+0

不是我所知道的,也许你可以找到一个库,但我真的怀疑它,如果你知道如何在计算器中计算它的功能,那么所有功能都是存在的。这里列出了所有的数学函数:https://docs.python.org/3/library/math.html – MCManuelLP