簡體   English   中英

切線問題

[英]Tangent Problems

好的,我在說的切線不是切線逆,而是能夠解決直角三角形中缺少的角度長度的切線。 到目前為止,我已經能夠將另一條腿除以相鄰的長度。

#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語句,並說不。 請幫忙! 非常感謝。

四舍五入后,您無需更新tan 請注意,float對象是不可變的,並且round返回數字類型。 四舍五入后沒有tan的就地更新。

您需要分配將返回的float對象重新綁定到tan

tan = round(float(tan), 3)

這里有多個問題:

  • tan是一個字符串,因此永遠不會等於浮點數。 請注意,您只分配一次tan。 舍入運算的輸出將被打印或丟棄,但不會存儲在tan 您可能想要這樣的東西:

     tan = round(float(y) / float(x), 3) 
  • 您可以使用==與浮點數進行比較。 永遠不要檢查浮點數是否相等! (除非您將它們分配為文字。)您應該始終檢查兩個數字的接近程度:

     if abs(tan - 1.333) < 1e5: 
  • 另外:不要將任何東西都轉換為字符串,除非您需要對字符串進行操作(例如,對其進行索引)。 為什么不使用Python 數學函數?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM