问题描述
我试着用规则找到一个三角形的角度: 如果三个数字的总和 = 180,则再次检查: 如果三角形的三个角形成大小在0°到90°之间的锐角,则打印“锐角”字样 如果其中一个角是大小在90°到180°之间的钝角,则打印“钝角”字样 如果其中一个角是大小为90°的直角,则打印“carpenter's triangle” 如果三个数字的总和不是 180,则打印“NOT TRIANGLE”字样
A = int(input("enter the number of 1 : "))
B = int(input("enter the number of 2 : "))
C = int(input("enter the number of 3 : "))
if(A+B+C <= 90):
print("acute angle")
if( 90>A<=180 or 90>B<=180 or 90>C<=180 ):
print("obtuse angle")
if(A+B+C > 180):
print("NOT TRIANGLE")
break
if(A==90 or B==90 or C==90):
print("carpenter's triangle")
我需要你的意见来修复这个程序
解决方法
嗯,如果我没听错的话:
if A+B+C != 180:
#Cannot be a triangle
print("Not triangle,please enter new values")
else:
#We can proceed as its a viable entry
if A < 90 and B < 90 and C < 90:
print("Acute Angle")
elif A == 90 or B == 90 or C == 90:
print("Carpenter's triangle")
elif A > 90 or B > 90 or C > 90:
print("Obtuse angle")
else:
print("Some other scenario we haven't accounted for")
按照您的逻辑,您永远不会进入有效三角形的打印(“锐角”)场景。
,您的 if 条件不正确。并且您应该使用 elif 而不是 if 而不先使用 if。因为三角形不能同时是锐角和钝角。
A = int(input("enter the number of 1 : "))
B = int(input("enter the number of 2 : "))
C = int(input("enter the number of 3 : "))
if A+B+C != 180 or A<0 or A>180 or B<0 or B>180 or C<0 or C>180:
print("NOT TRIANGLE")
elif A<90 and B<90 and C<90:
print("acute angle")
elif (A>90 and A<180) or (B>90 and B<180) or (C>90 and C<180):
print("obtuse angle")
elif A==90 or B==90 or C==90:
print("carpenter's triangle")
else:
print("Wut?")
,
这是您可以尝试的一种解决方案:
# If sum of angles makes upto 180; then a triangle
if(A+B+C == 180):
# if atleast one side is 90; then Carpenter's triangle
if(A==90 or B==90 or C==90):
print("Carpenter's triangle")
# if all sides are less than 90; then Accute triangle
elif(A<90 and B<90 and C<90):
print("Accute triangle")
# if atleast one side is greater than 90; then Obtuce triangle
elif(90>A<=180 or 90>B<=180 or 90>C<=180):
print("Obtuse triangle")
else:
print('Not a triangle')
-
如果至少有一个角度是 90 度;那么它不可能是急性的或钝性的 三角形
-
如果所有边都小于90;那么它不可能是一个钝角三角形
-
注意 if-else 流程