我必须写一个关于三角形的程序。我需要两个文件,一个模块文件和一个带有main方法的可执行文件。
模块文件(定义两个函数): 将三角形的三边作为参数的一个函数。如果边定义直角三角形则返回True,如果不定义则返回false。
另一个函数必须使用Heron公式计算并返回三角形的面积。在线了解苍鹭的配方。
主文件: 提示用户输入三角形的三边(最长的一边)。仅使用整数。确保没有边长于其他两边的总和。调用返回布尔值的函数并使用它来输出三角形是否是直角三角形。调用返回区域的函数。显示返回的值,带有两位小数。
我有一些我制作的代码,但是当我在显示区域后运行它时,我看到它没有。我不知道从哪里得到它。此外,我试图确保没有任何一方比另外两方的总和更长,并提示用户重新进入vaules。我不知道我是否使用了正确的语句,因为python没有认识到它。
这是我的代码:
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
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:', area(a, b, c))
print(right_tri(a, b, c))
print(is_sum(a, b, c))
main ()
答案 0 :(得分:0)
关系运算符的优先级高于加法,因此在is_sum的第2行中需要使用方括号 a>(b + c)等 您的代码可能一直都在到达print语句,并且没有返回会导致默认的返回值None