python三角形角度返回null

时间:2017-09-03 16:14:10

标签: python

一个函数,它从边长度返回三角形的角度值,结果是从最低到最高排序的整数

from math import acos, degrees

def angles(a, b, c):
    alpha = beta = gamma = 0
    if a !=0 and b != 0 and c != 0:
        if a + b < c or a + c < b or b + c < a:
            return [alpha, beta, gamma]
        else:
            alpha = int(degrees(acos(( c*c + b*b - a*a ) / (2 * c * b))))
            beta = int(degrees(acos(( a*a + c*c - b*b ) / (2 * a * c))))
            gamma = int(degrees(acos(( a*a + b*b - c*c ) / (2 * a * b))))
            return list.sort([alpha, beta, gamma])

为什么我会得到None

2 个答案:

答案 0 :(得分:3)

list.sort就地对列表进行排序并返回None。您可以改为使用sorted

return sorted([alpha, beta, gamma])

答案 1 :(得分:2)

试试这段代码:

来自math import acos,degrees

def angles(a, b, c):
    alpha = beta = gamma = 0
    if a !=0 and b != 0 and c != 0:
        if a + b < c or a + c < b or b + c < a:
            return [alpha, beta, gamma]
        else:
            alpha = int(degrees(acos(( c*c + b*b - a*a ) / (2 * c * b))))
            beta = int(degrees(acos(( a*a + c*c - b*b ) / (2 * a * c))))
            gamma = int(degrees(acos(( a*a + b*b - c*c ) / (2 * a * b))))
            lst = [alpha, beta, gamma]
            lst.sort()
            return lst

print angles(4,4,4)