2016-09-29 52 views
-1
from math import sqrt 
a = raw_input("First length: ") 
b = raw_input("Second length: ") 
c = raw_input("Third length: ") 
if a >0 and b >0 and c >0 and a + b > c and a + c > b and b + c > a : 
    if a == b == c: 
     print "Equilateral triangle." 
    elif a == b and a != c and b != c: 
     print "Isosceles triangle." 
    elif (a==sqrt(b*b+c*c) or b==sqrt(a*a+c*c) or c==sqrt(a*a+b*b)): 
     print "Right triangle." 
    else: 
     print "Simple triangle." 
else: 
    print "The shape is not a triangle." 

當我插入「2」,「2」和「2」時,一切正常,但是當我輸入「3」,「4」和「5」我得到: 「形狀不是三角形。」 。你能幫我找到問題嗎? (我現在看到了,我能找到的另一篇文章的解決方案,但我......不知道這個問題)無法驗證一個形狀是否爲三角形

+0

首先,你搞砸了畢達哥拉斯定理:) –

+2

'raw_input'返回一個字符串,而不是數字。你需要'int(raw_input(「First length:」))'或'float(raw_input(「First length:」))''。它適用於相同的輸入,因爲您正在比較相同的字符串。一旦這些數字的字符串表示發生變化,那麼'elif'子句都不爲真 – roganjosh

+0

'b!= c'檢查是多餘的;如果前兩次比較是真的,則保證是真的。 – chepner

回答

0

在Python 2.7

raw_input返回str值。要麼使用input() OR類型轉換raw_input()int爲:

int(raw_input("First length: ")) 

在Python 3

只有input()執行相同的操作的Python 2.x中的raw_input()raw_input不存在Python 3

注意:根據Python 2.7 Document

input('Something Something ...')相當於eval(raw_input('Something Something ...'))

由於安全原因,我們不應該在代碼中使用eval。閱讀:Is using eval in Python a bad practice?

+1

不建議'輸入'。 Python 3擺脫了它的原因。 – chepner

+0

但用戶提到標籤爲'python 2.7' –

+0

@MoinuddinQuadri爲true,並且存在'input'。但是,它在Python 2.7中的輸入上調用'eval()','eval'不安全。 – roganjosh