用python判断三角形的形状
# coding:utf-8 class point:
def __init__(self,x,y,name):
self.x = x
self.y = y
self.name = name '''两点距离公式'''
def distance(self,p2):
self.d=((self.x-p2.x)**2+(self.y-p2.y)**2)**0.5
return self.d '''获取两点距离'''
def getd(self,p2):
self.distance(p2)
print('点 ({},{}) 与 ({},{}) 的距离是 {} '.format(self.x,self.y,p2.x,p2.y,self.d)) '''判断这三点能否形成一个三角形'''
def istriangle(self,p2,p3):
self.l_list=[]
self.l_list.append(self.distance(p3))
self.l_list.append(p2.distance(p3))
self.l_list.append(self.distance(p2))
'''线段长度由小到大排序'''
self.l_list.sort()
if (self.l_list[0]+self.l_list[1]>self.l_list[2]) and (self.l_list[1]+self.l_list[2]>self.l_list[0]) and (self.l_list[2]+self.l_list[0]>self.l_list[1]):#长度判断
return '能构成三角形'
else:
return '不能构成' '''判断是哪种三角形'''
def whichtriangle(self,p2,p3):
result=self.istriangle(p2,p3)
if result=='不能构成':
return print('所以点',self.name,p2.name,p3.name,'无法构成一个三角形')
if self.l_list[0]**2+self.l_list[1]**2>self.l_list[2]**2:#锐角三角形
print('所以点',self.name,p2.name,p3.name,'能构成一个锐角三角形')
elif self.l_list[0]**2+self.l_list[1]**2==self.l_list[2]**2:#直角三角形
print('所以点',self.name,p2.name,p3.name,'能构成一个直角三角形')
elif self.l_list[0]**2+self.l_list[1]**2<self.l_list[2]**2:#钝角三角形
print('所以点',self.name,p2.name,p3.name,'能构成一个钝角三角行') if __name__ == '__main__':
'''第一组输入'''
try:
p1 = point(0, A, 'p1') #输入坐标点P1的x,y坐标及点的名字
p2 = point(1, 3, 'p2') #输入坐标点P2的x,y坐标及点的名字
p3 = point(9, 7, 'p3') #输入坐标点P3的x,y坐标及点的名字
p1.getd(p2) #计算点p1与p2的距离,也就是三角形的一条边
p2.getd(p3) #计算点p2与p3的距离,也就是三角形的一条边
p3.getd(p1) #计算点p3与p1的距离,也就是三角形的一条边
p1.whichtriangle(p2, p3) #任意2边的平方之和与第三边平方做比较,大于是锐角,等于是直角,小于是钝角
print() #输入结果之间加一个空行
except:
print("输入的值错误")
用python判断三角形的形状的更多相关文章
- python判断字符串
python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...
- 【Python备忘】python判断文件和文件夹是否存在
python判断文件和文件夹是否存在 import os os.path.isfile('test.txt') #如果不存在就返回False os.path.exists(directory) #如果 ...
- 【C语言】判断三角形类型
根据输入的三角形的三边判断三角形的类型,并输出其面积和类型. #include<stdio.h> #include<stdlib.h> #include<math.h&g ...
- python 判断连个 Path 是否是相同的文件夹
python 判断连个 Path 是否是相同的文件夹 import os os.path.normcase(p1) == os.path.normcase(p2) normcase() 在 windo ...
- Python3:判断三角形的类型
# 判断三角形类型def triangle(a,b,c): if a>0 and b>0 and c>0: if a+b>c and b+c>a and a+c>b ...
- Python判断列表是否已排序的各种方法及其性能分析
目录 Python判断列表是否已排序的各种方法及其性能分析 声明 一. 问题提出 二. 代码实现 2.1 guess 2.2 sorted 2.3 for-loop 2.4 all 2.5 numpy ...
- python 判断变量是否存在 防止报错
Python判断变量是否存在 方法一:使用try: ... except NameError: .... try: var except NameError: var_exists = False e ...
- python 判断是否为中文
python在执行代码过程是不知道这个字符是什么意思的.是否是中文,而是把所有代码翻译成二进制也就是000111这种形式,机器可以看懂的语言. 也就是在计算机中所有的字符都是有数字来表示的.汉字也是有 ...
- (转)python 判断数据类型
原文:https://blog.csdn.net/mydriverc2/article/details/78687269 Python 判断数据类型有type和isinstance 基本区别在于: t ...
- python判断字符串是否为空的方法s.strip()=='' if not s.strip():
python 判断字符串是否为空用什么方法? 复制代码 s=' ' if s.strip()=='': print 's is null' 或者 if not s.strip(): p ...
随机推荐
- (四)用go实现红黑树
本篇文章我们用go来实现红黑树,部分参考资料如下: https://www.cnblogs.com/skywang12345/p/3245399.html#!comments https://blog ...
- Go--求数组奇偶数之和
package main //申明main包 import "fmt" // 导入fmt标准库 func main() { arr := [...]int{01, 11, 22, ...
- 【java数据结构与算法】选择排序
选择排序原理剖析: 假设数组arr使用选择排序 每一轮选出数组最小的元素 arr.lenth个元素,只需要找出arr.length-1个元素的正确位置 选择排序便进行结束 外层for循环控制选择排序的 ...
- windows用curl报错
https://www.shuzhiduo.com/A/kmzLRmgl5G/ IE浏览器 -> 设置 -> Internet选项 -> 安全 -> 本地Internet -& ...
- selenium最常用的基本方法
1.打开,关闭浏览器 打开chrome浏览器:webdriver.Chorme() 打开Firefox浏览器:webdriver.Firefox() 关闭当前浏览器窗口:driver.close() ...
- 解决pdf电子签章显示问题(电子发票)
在/build/pdf.worker.js注释掉一行代码 if (data.fieldType === "Sig") { data.fieldValue = null; // 注释 ...
- 常见的abd命令
https://blog.csdn.net/qq_34512207/article/details/125283285
- OS X Maven 安装与使用简介
Java真的很重很复杂,连项目构建和编译都得专门拉出来学,这里整理一下在OS X上使用Maven的注意事项. 一.安装 [bash] 1.从http://maven.apache.org/downlo ...
- 【Anaconda】为右键菜单添加“当前位置开启Anaconda Prompt”
Stack Overflow 上查找到该解决方法:『Adding "Open Anaconda Prompt here" to context menu (Windows) - S ...
- 记D365开发的最佳实践
前端JS 不同的需求应该划分模块,以便日后修改,也是为了职责分离,模块分离,日后如果想分离到单独的JS文件里面也是比较方便: 对于公共的查询函数,应该做缓存,优先使用sessionStorage. 多 ...