Python3如何判斷三角形的型別
阿新 • • 發佈:2020-04-13
# 判斷三角形型別
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: if a == b and b == c: return ("這是等邊三角形") elif a == b or b == c or c == a: return("這是等腰三角形") else: return("這是不規則三角形") elif a+b==c or b+c==a or a+c==b: return("這是個直角三角形") else: return('這好像不是個三角形') else: return("請輸入大於0的數字")
補充知識:python:輸入三個數判斷是什麼三角形
剛剛學習Python,歡迎大家指點
#Filename:Triangle #Function:Judgment triangle #Author:Judy #Time:2018.9.26 a=int(input("Please input the first side:")) #輸入第一條邊 b=int(input("Please input the second side:")) #輸入第二條邊 c=int(input("Please input the third side:")) #輸入第三條邊 if (a+b>c) and (a+c>b) and (b+c>a): #判斷是否是三角形 if a==b==c: print("This is a equilateral triangle") #等邊三角形 elif (a==b or a==c or b==c): print("This is a isosceles triangle") #等腰三角形 elif (a*a+b*b==c*c) or (a*a+b*b==c*c) or (a*a+b*b==c*c): print("This is a right triangle") #直角三角形 else: print("This is a scalene triangle") #不規則三角形 else : print("This isn't a triangle") #不是三角形
注意點:不能直接使用a=input(),輸入3,用a=input(),a=‘3',型別為string型別,不能進行相乘
使用[a,c]元組進行輸入,不能直接轉換成int,因為元組最多隻能int兩個引數
以上這篇Python3如何判斷三角形的型別就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。