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」我得到: 「形狀不是三角形。」 。你能幫我找到問題嗎? (我現在看到了,我能找到的另一篇文章的解決方案,但我......不知道這個問題)無法驗證一個形狀是否爲三角形
首先,你搞砸了畢達哥拉斯定理:) –
'raw_input'返回一個字符串,而不是數字。你需要'int(raw_input(「First length:」))'或'float(raw_input(「First length:」))''。它適用於相同的輸入,因爲您正在比較相同的字符串。一旦這些數字的字符串表示發生變化,那麼'elif'子句都不爲真 – roganjosh
'b!= c'檢查是多餘的;如果前兩次比較是真的,則保證是真的。 – chepner