无法验证形状是否为三角形

时间:2016-09-29 18:31:25

标签: python python-2.7

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"我明白了: "形状不是三角形。" 。你能帮我找到问题吗? (我现在看到我可以在另一篇文章中找到解决方案,但我......并不知道这个问题)

1 个答案:

答案 0 :(得分:0)

在Python 2.7中

raw_input返回str值。使用input()或将raw_input()int类型转换为:

int(raw_input("First length: "))

在Python 3中

只有input()执行与Python 2.x的raw_input()相同的操作,并且Python 3中不存在raw_input

注意 :根据Python 2.7 Document

  

input('Something Something ...')相当于eval(raw_input('Something Something ...'))

出于安全原因,我们不应该在代码中使用eval。阅读:Is using eval in Python a bad practice?