查找三角形是否为直角 [英] Finding if a triangle is right-angled or not
问题描述
如果给定边长x,y和z,三角形是否为直角,则此基于Python 3的函数将返回。我在简化条件声明时遇到问题。该功能应该检查锐角,直角,钝角,斜角,等腰角和等边角,还是可以跳过的条件?
This Python 3 based function returns if a triangle is or isn't right-angled given side lengths x, y, and z. I'm having an issue simplifying the conditional statement. Should this function check for acute, right, obtuse, scalene, isosceles, and equilateral angles, or are there conditions I can skip? Any feedback is appreciated.
def right_angled(x, y, z):
"""This function returns if a triangle is or isn't
right-angled given side lengths x, y, and z."""
p = x + y + z #triangle perimeter
a_sym = p / 180 #triangle perimeter divided by 180
one = x * a_sym #angle one
two = y * a_sym #angle two
three = z * a_sym #angle three
if one and two or one and three or two and three == 90:
return "The triangle is right-angled."
elif one and two and three == 180:
return "The triangle is right-angled." #next conditional(s)?
else:
return "The triangle is not right-angled."
print(right_angled(4, 5, 6))
推荐答案
您的功能完全错误。
找不到角度作为边与周长的比率。
You cannot find angle as ratio of a side and perimeter.
表达式一个和两个
不计算总和-和
这里是逻辑(布尔)运算符。
Expression if one and two
does not calculate sum - and
here is logical (boolean) operator.
要确定矩形是否正确,可以利用勾股定理
To find whether rectangle is right, you can exploit Pythagorean theorem
def right_angled(a, b, c):
if (a*a+b*b==c*c) or (c*c+b*b==a*a) or (a*a+c*c==b*b) :
return "The triangle is right-angled."
else:
return "The triangle is not right-angled."
或者只返回布尔结果
return (a*a+b*b==c*c) or (c*c+b*b==a*a) or (a*a+c*c==b*b)
这篇关于查找三角形是否为直角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!