python中的三角形

我有我的程序有问题,我必须做...这里是说明:编写一个名为triangle.py的程序,该程序使用一个返回值函数,该函数将三角形边的长度作为参数,并返回区域和三角形的周长。 提示用户在主功能键盘上输入边长,然后调用第二个功能。 将区域和周边显示精确到小数点后一位。 注意:使用Heron的公式来查找区域。 我做到了,但它的工作原理是他希望我们使用一个函数返回面积和周长..所以我重新做了一个函数的程序,但我不断收到一个错误,说每个没有定义...我尝试从字面上解决问题,网上查找,没有任何工作。 :(这里是我的代码:

def main():

    a = float(input('Enter the length of the first side: '))
    b = float(input('Enter the length of the second side: '))
    c = float(input('Enter the length of the third side: '))



    print('The perimeter of the triangle is: ', format(per(a, b, c),',.1f'))

    print('The area of the triangle is: ', format(area(a, b, c),',.1f'))

def area_per(a, b, c):

    per = a + b + c
    s = per / 2
    area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
    return area_per


main()

这里的问题是你定义的“per”变量是area_per函数的本地变量。

如果你想要合适的范围,你需要创建另一个功能。

def main():

    a = float(input('Enter the length of the first side: '))
    b = float(input('Enter the length of the second side: '))
    c = float(input('Enter the length of the third side: '))



    print('The perimeter of the triangle is: ', format(per(a, b, c),',.1f'))

    print('The area of the triangle is: ', format(area_per(a, b, c),',.1f'))

def area_per(a, b, c):

    p = per(a,b,c)
    s = p / 2
    area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
    return area

def per(a,b,c):
    return a+b+c  

main()
链接地址: http://www.djcxy.com/p/53137.html

上一篇: Triangle in python

下一篇: Python: how to avoid writing numerous if/elifs in a function?