Python判斷三段線能否構成三角形的代碼
我就廢話不多說了,還是直接看代碼吧!
#!/usr/bin/env python3#coding = utf-8def is_triangle(a=0, b=0, c=0): #abc 三條邊長 A = [a,b,c] A.sort() #升序排序 if A[2] < A[1] +A[0]: print('{} is triangle'.format(A)) else: print('不構成三角')def triangle(f): a = float(input('第一條邊是 = ')) b = float(input('第二條邊是 = ')) c = float(input('第三條邊是 = ')) f(a, b, c)triangle(is_triangle) # 常規函數的調用
補充知識:python編程:判斷輸入的邊長能否構成三角形 如果能則計算出三角形的周長和面積
看代碼吧!
def main(): a = float(input(’a = ’)) b = float(input(’b = ’)) c = float(input(’c = ’)) if a + b > c and a + c > b and b + c > a: print(’周長: %f’ % (a + b + c)) p = (a + b + c) / 2 area = math.sqrt(p * (p - a) * (p - b) * (p - c)) print(’面積: %f’ % (area)) else: print(’不能構成三角形’)if __name__ == ’__main__’: main()
以上這篇Python判斷三段線能否構成三角形的代碼就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章: