张金龙的博客分享 http://blog.sciencenet.cn/u/zjlcas 物种适应性、分布与进化

博文

Python举例: 输入三角形边长,判断所生成三角形和面积

已有 9526 次阅读 2015-1-4 13:05 |个人分类:软件介绍|系统分类:科研笔记|关键词:学者

import math

a,b,c = input("Input the length of sides separated by comma:")

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

   p = (a + b + c)/2.0

   temp = p * (p - a) * (p - b) * (p - c)

   area = math.sqrt(temp)

   if a == b or b == a or c == a: 

       if a == b == c:

           res = "equilateral triangle"

       elif math.fabs(a**2 + b**2 - c**2) < 1e-6 or math.fabs(b**2 + c**2 - a**2) < 1e-6 or math.fabs(c**2 + a**2 - b**2) < 1e-6:

           res = "isosceles triangle"

       elif math.fabs(a**2 + b **2 - c**2) < 1e-6 or (b**2 + c **2 - a**2) < 1e-6  or (c**2 + a **2 - b**2) < 1e-6:

           res = "rightangled triangle"

   else: 

       res = "Common triangle"

else: 

   res = "Not triangle"

   

if res != "Not triangle":

   print("Area for this triangle is", area)

   print("It is a", res)

else:

   print("Not a triangle")




https://m.sciencenet.cn/blog-255662-856325.html

上一篇:用QGIS生成地图一副完整地图
下一篇:世上再无Stefan

1 蒋迅

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-29 22:54

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部