0

I made this calculator to try and calculate trigonometry in python. I've only just started learning how to use this program and I haven't been able to find anything so far that makes sense to me. The problem with this is that I keep getting different answers to those that come up on my scientific calculator.

while True: 

    print('type "sine1" to find the value of the Opposite')
    print('type "sine2" to find the value of the Hypotenuse')
    print('type "cosine1" to find the value of the Adjacent')
    print('type "cosine2" to find the value of the Hypotenuse')
    print('type "tangent1" to find the value of the Opposite')
    print('type "tangent2" to find the value of the Adjacent')
    user_input = input(": ")

    from math import sin, cos, tan


    if user_input == 'sine1':
        degrees = float(input('Enter the degrees: '))
        hypotenuse = float(input('Enter the value of the hypotenuse: '))
        result = str(hypotenuse * sin(degrees))
        print('The answer is ' + result)
        print(input('Press enter to quit: '))
        break

    elif user_input == 'sine2':
        degrees = float(input('Enter the degrees: '))
        opposite = float(input('Enter the value of the opposite: '))
        result = str(opposite / sin(degrees))
        print('The answer is ' + result)
        print(input('Press enter to quit: '))
        break

    elif user_input == 'cosine1':
        degrees = float(input('Enter the degrees: '))
        hypotenuse = float(input('Enter the value of the hypotenuse: '))
        result = str(hypotenuse * cos(degrees))
        print('The answer is ' + result)
        print(input('Press enter to quit: '))
        break

    elif user_input == 'cosine2':
        degrees = float(input('Enter the degrees: '))
        adjacent = float(input('Enter the value of the adjacent: '))
        result = str(adjacent / cos(degrees))
        print('The answer is ' + result)
        print(input('Press enter to quit: '))
        break

    elif user_input == 'tangent1 ':
        degrees = float(input('Enter the degrees: '))
        adjacent = float(input('Enter the value of the adjacent: '))
        result = str(hypotenuse * tan(degrees))
        print('The answer is ' + result)
        print(input('Press enter to quit: '))
        break

    elif user_input == 'tangent2':
        degrees = float(input('Enter the degrees: '))
        opposite = float(input('Enter the value of the opposite: '))
        result = str(adjacent / cos(degrees))
        print('The answer is ' + result)
        print(input('Press enter to quit: '))
        break

    else:
        print('invalid input, please close the program and try again... maybe learn how to spell first :P')
        print(input('press enter to quit'))
2
  • 2
    Hi welcome to StackOverflow. So what exactly is your question? You need to narrow it down to a specific problem to get a positive reaction from most people on this site. Please update the question by clicking the edit button below your post. Thanks! Commented Jun 6, 2018 at 3:11
  • Trigonometric functions in every language use radians, not degrees. Make sure you convert before you give an angle value to a function.
    – duffymo
    Commented Jun 6, 2018 at 12:05

2 Answers 2

5

All trigonometric function from the math module require their argument to be in radians, not in degrees. You can use math.radians to do the conversion.

import math

degrees = 90
radians = math.radians(degrees)

print(math.sin(radians)) # 1.0
2

Python's trig functions assume that the input is in radians, and you're entering degrees.

First convert your degrees to radians by multiplying degrees by math.pi/180.0 and see if those match better.

7
  • Excellent edit from math.py to math.pi. I thought my eyes were deceiving me for a moment there. XD
    – Andrew Fan
    Commented Jun 6, 2018 at 2:56
  • Just tried what you suggested, ended up getting this error: Commented Jun 6, 2018 at 2:58
  • Traceback (most recent call last): File "..\Playground\", line 17, in <module> result = str(hypotenuse * sin(degrees * math.py/180)) NameError: name 'math' is not defined Commented Jun 6, 2018 at 2:58
  • Thanks, will try that again Commented Jun 6, 2018 at 2:58
  • 1
    Unless you need to work with Python 1.1 or something, it's better to use math.radians than to do the math explicitly. Sure, the possible rounding error of one ULP probably isn't going to make a difference, and the performance cost of a float division definitely won't make a difference in CPython, but… why make it more complicated only to introduce two flaws, no matter how trivial those flaws are?
    – abarnert
    Commented Jun 6, 2018 at 3:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.