三角不等式

时间:2017-02-12 18:06:48

标签: function python-3.x return-value

我必须编写一个程序,它接受三角形边的用户输入,如果它是一个直角三角形,则打印该区域。我的任务还要求我们确保没有任何一方长于其他双方的总和。我试图找出如何使我的代码工作的一方不超过其他双方的总和,并提示用户重新开始,但我不知所措。我也有一个问题,当我的程序运行时,它打印单词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 ()  

2 个答案:

答案 0 :(得分:0)

假设你的代码缩进是正确的(因为python依赖它)以下应该是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提供斜边。

答案 1 :(得分: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'