简体   繁体   中英

Tangent Problems

Okay, the tangent I'm talking about isn't tangent inverse, but the tangent that is able to solve missing angle lengths in a right triangle. So far, I've been able to divide the opposite leg by a adjacent length.

#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);

For some reason, the program will only use the else statement, and say nope. Please help! THANKS so much in advance.

You're not updating tan after rounding . Note that float objects are immutable and round returns a numeric type; there isn't an in-place update of tan after rounding.

You need an assignment to rebind the returned float object to tan :

tan = round(float(tan), 3)

There are multiple problems here:

  • tan is a string, so it will never equal a floating point number. Note that you only assign to tan once. The output of your rounding operations is either printed or discarded but not stored in tan . You probably want something like this:

     tan = round(float(y) / float(x), 3) 
  • You compare against a floating point number with == . You should never check equality with floating point numbers! (unless you assign them as literals.) You should instead always check how close two numbers are:

     if abs(tan - 1.333) < 1e5: 
  • Also: Don't convert anything to strings, unless you need to operate on the string (eg, index it). Why don't you use Python math functions?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM