python判断三角形是否直角_查找三角形是否为直角

该Python函数根据边长x, y, z判断一个三角形是否为直角三角形。文章讨论了如何简化条件语句,并提供了使用勾股定理的正确实现。" 98883252,8511896,TCP协议实现文件高效传输,"['网络编程', '文件操作', 'TCP/IP', '数据传输']
摘要由CSDN通过智能技术生成

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

解决方案

Your function is completely wrong.

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."

Or just return boolean result

return (a*a+b*b==c*c) or (c*c+b*b==a*a) or (a*a+c*c==b*b)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值