2017-02-12 39 views
-1

我必須編寫一個程序,該程序需要用戶輸入三角形的邊,並打印它是否爲直角三角形以及該區域是什麼。我的任務還要求我們確保沒有任何一方長於其他雙方的總和。我試圖弄清楚如何讓我的代碼不會比其他雙方的總和長得多,並且提示用戶重新開始,但我不知所措。我也遇到了一個問題,當我的程序運行時,它打印出none,我認爲它與我的def right_tri函數有關,但我不確定爲什麼它會這樣做。三角不等式

這是我的代碼任何幫助將不勝感激!

def area(a, b, c): 
    s = (a + b + c)/2 

    area = (s*(s-a)*(s-b)*(s-c)) **0.5 
    return format(area, '.2f') 

def right_tri(a, b, c): 
    if (b**2 + c**2 == a**2): 
     print('Is a right triangle') 

else: 
     print('Is not a right triangle') 

def main() : 

    a = int(input('Enter longest side of the triangle')) 
    b = int(input('Enter the second side of the triangle')) 
    c = int(input('Enter the thrid side of the triangle')) 

    print('Area is:', triangle.area(a, b, c)) 
    print(triangle.right_tri(a, b, c)) 
    print(is_sum(a, b, c)) 

def is_sum(a, b, c): 
    if (a > b + c) or (b > a + c) or (c > a + b): 
     print ('One side is longer than the sum of the other two sides') 
else: 
    return True 

main() 
+0

對於任何一個三角形**這是不可能的一邊是長於其他雙方**的總和!絕對沒有必要測試這種情況。 –

回答

0

假設你的代碼的縮進是正確的(如蟒蛇依賴於它)以下應該是right_tri實施,

def right_tri(a, b, c): 
    if ((b**2 + c**2 == a**2) || (a**2 + c**2 == b**2) || (b**2 + a**2 == c**2)): 
     print('Is a right triangle') 

    else: 
     print('Is not a right triangle') 

原因多個條件是因爲,你永遠不知道,如果用戶正在給斜邊abc

0

在right_tri功能,你需要使用一個return語句,就像

def right_tri(a, b, c): 
     if (b**2 + c**2 == a**2): 
      return 'Is a right triangle' 
     else: 
      return 'Is not a right triangle'