2017-03-15 40 views
-1

好的,我所说的切线不是正切倒转,而是能够解决直角三角形中缺失角度长度的切线。到目前为止,我已经能够将相反的腿分开一段相邻的长度。切线问题

#this is for a 6,8, 10 triangle! 
#y is 8, x is 6 
#if you look on the table, 1.3333 is near 53 degrees 

if(a==b): 
print("This is a right triagle b/c the legs are equal to the hypotnuse!") 

tan = input("Would you like to find the tangent of angle A? : ") 
if(tan=="Yes"): 
    print("Lets do this!") 
    #works with 6,8,10 triangle for now! 
    print(y,"/",x) 
    tan = (str(float(y)/float(x))) 
    round(float(tan),3) 
    print(round(float(tan),3)) 
    print("Looking for rounded tan in table!") 
    #remember that 1.333 is near 53 degrees (see on trig table) 
    if(tan == 1.333): 
     print("<A == ", 53) 
     print("<B == 90") 
     c = str(int(53)+int(90)) 
     c2 = str(int(180)-int(c)) 
     print("<C == ",c2) 
    else: 
     print("Nope") 

elif(tan=="No"): 
    print("See you!") 
    exit(0); 

由于某些原因,程序将只使用else语句,并说nope。请帮忙!提前感谢。

回答

2

您没有更新tan四舍五入。请注意浮动对象是不可变的round返回一个数字类型;经过四舍五入后,没有就地更新tan

你需要一个任务返回的浮点对象重新绑定到tan

tan = round(float(tan), 3) 
1

这里还有多个问题:

  • tan是一个字符串,所以它永远不会等于一个浮点数。请注意,您只能分配一次。舍入操作的输出可以打印或丢弃,但不会存储在tan中。你可能想是这样的:

    tan = round(float(y)/float(x), 3) 
    
  • 你比较反对一个浮点数与==。你应该从来没有与浮点数检查相等! (除非你将它们分配为文字。)而应该经常检查两个数字的接近程度:

    if abs(tan - 1.333) < 1e5: 
    
  • 另外:不要转换什么字符串,除非你需要在字符串操作(例如,指数它)。为什么不使用Python math函数?