python中的三角形
我的程序有问题,我必须做。。。以下是说明:编写一个名为triangle.py的程序,该程序使用一个值返回函数,该函数将三角形边的长度作为参数,并返回三角形的面积和周长。提示用户在main函数中输入键盘的边长,然后调用第二个函数。显示精确到小数点后一位的面积和周长。注:使用Heron公式计算面积。我做到了,但他希望我们使用一个函数,返回面积和周长。。所以我用一个函数重新做了这个程序,但是我一直得到一个错误,说per没有定义。。。我想尽一切办法来修复它,在网上看了看,什么也没用(这是我的密码:python中的三角形,python,geometry,Python,Geometry,我的程序有问题,我必须做。。。以下是说明:编写一个名为triangle.py的程序,该程序使用一个值返回函数,该函数将三角形边的长度作为参数,并返回三角形的面积和周长。提示用户在main函数中输入键盘的边长,然后调用第二个函数。显示精确到小数点后一位的面积和周长。注:使用Heron公式计算面积。我做到了,但他希望我们使用一个函数,返回面积和周长。。所以我用一个函数重新做了这个程序,但是我一直得到一个错误,说per没有定义。。。我想尽一切办法来修复它,在网上看了看,什么也没用(这是我的密码: de
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”变量是每个函数的区域_的局部变量 如果您想要正确的作用域,您需要创建另一个位于该作用域之外的函数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()
你到底是什么意思?你现在能说得更详细一点吗?对不起,我刚才听到了。可能是重复的