"""
Trigonometry Mastery - Console App
----------------------------------
Features:
1. Degree <-> Radian conversion
2. Calculate sin, cos, tan
3. Calculate inverse trig functions
4. Solve right triangles
5. Show special angle values
6. Trigonometry quiz
7. Identity checker
Educational purpose only.
"""
import math
import random
import time
# -----------------------------------
# Utility Functions
# -----------------------------------
def line():
print("-" * 60)
def safe_float_input(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print("Please enter a valid number.")
def safe_int_input(prompt):
while True:
try:
return int(input(prompt))
except ValueError:
print("Please enter a valid integer.")
def round_val(value, digits=6):
return round(value, digits)
# -----------------------------------
# Degree / Radian Conversion
# -----------------------------------
def degrees_to_radians():
line()
deg = safe_float_input("Enter angle in degrees: ")
rad = [Link](deg)
print(f"{deg} degrees = {rad} radians")
line()
def radians_to_degrees():
line()
rad = safe_float_input("Enter angle in radians: ")
deg = [Link](rad)
print(f"{rad} radians = {deg} degrees")
line()
# -----------------------------------
# Basic Trigonometric Functions
# -----------------------------------
def trig_values():
line()
deg = safe_float_input("Enter angle in degrees: ")
rad = [Link](deg)
sin_val = [Link](rad)
cos_val = [Link](rad)
print(f"\nFor angle = {deg}°")
print(f"sin({deg}) = {round_val(sin_val)}")
print(f"cos({deg}) = {round_val(cos_val)}")
# tan undefined when cos is ~0
if abs(cos_val) < 1e-10:
print(f"tan({deg}) is undefined")
else:
tan_val = [Link](rad)
print(f"tan({deg}) = {round_val(tan_val)}")
line()
# -----------------------------------
# Inverse Trigonometric Functions
# -----------------------------------
def inverse_trig():
line()
print("Choose inverse function:")
print("1. sin⁻¹")
print("2. cos⁻¹")
print("3. tan⁻¹")
choice = input("Enter choice: ").strip()
if choice == "1":
value = safe_float_input("Enter value for arcsin (between -1 and 1): ")
if value < -1 or value > 1:
print("Invalid domain for arcsin.")
else:
rad = [Link](value)
deg = [Link](rad)
print(f"arcsin({value}) = {round_val(rad)} radians = {round_val(deg)}°")
elif choice == "2":
value = safe_float_input("Enter value for arccos (between -1 and 1): ")
if value < -1 or value > 1:
print("Invalid domain for arccos.")
else:
rad = [Link](value)
deg = [Link](rad)
print(f"arccos({value}) = {round_val(rad)} radians = {round_val(deg)}°")
elif choice == "3":
value = safe_float_input("Enter value for arctan: ")
rad = [Link](value)
deg = [Link](rad)
print(f"arctan({value}) = {round_val(rad)} radians = {round_val(deg)}°")
else:
print("Invalid choice.")
line()
# -----------------------------------
# Right Triangle Solver
# -----------------------------------
def solve_right_triangle():
"""
Assumes a right triangle.
Let:
- hypotenuse = c
- other sides = a, b
"""
line()
print("Right Triangle Solver")
print("1. Given two sides")
print("2. Given one side and one acute angle")
choice = input("Choose: ").strip()
if choice == "1":
print("\nEnter known values. Use 0 if unknown.")
a = safe_float_input("Side a: ")
b = safe_float_input("Side b: ")
c = safe_float_input("Hypotenuse c: ")
known = sum(1 for x in [a, b, c] if x > 0)
if known != 2:
print("Please provide exactly two known sides.")
line()
return
if c > 0 and (a >= c or b >= c):
print("Hypotenuse must be the longest side.")
line()
return
if a > 0 and b > 0:
c = [Link](a*a + b*b)
elif a > 0 and c > 0:
b_sq = c*c - a*a
if b_sq < 0:
print("Invalid side lengths.")
line()
return
b = [Link](b_sq)
elif b > 0 and c > 0:
a_sq = c*c - b*b
if a_sq < 0:
print("Invalid side lengths.")
line()
return
a = [Link](a_sq)
angle_A = [Link]([Link](a / c))
angle_B = 90 - angle_A
print("\nSolved Triangle:")
print(f"a = {round_val(a)}")
print(f"b = {round_val(b)}")
print(f"c = {round_val(c)}")
print(f"Angle A = {round_val(angle_A)}°")
print(f"Angle B = {round_val(angle_B)}°")
elif choice == "2":
print("\nEnter one side and one acute angle.")
print("Angle should be one of the non-right angles.")
angle = safe_float_input("Acute angle in degrees: ")
if angle <= 0 or angle >= 90:
print("Angle must be between 0 and 90.")
line()
return
print("\nWhich side do you know?")
print("1. Opposite")
print("2. Adjacent")
print("3. Hypotenuse")
side_choice = input("Choose: ").strip()
value = safe_float_input("Enter side length: ")
rad = [Link](angle)
if side_choice == "1":
a = value
c = a / [Link](rad)
b = c * [Link](rad)
elif side_choice == "2":
b = value
c = b / [Link](rad)
a = c * [Link](rad)
elif side_choice == "3":
c = value
a = c * [Link](rad)
b = c * [Link](rad)
else:
print("Invalid choice.")
line()
return
other_angle = 90 - angle
print("\nSolved Triangle:")
print(f"Opposite side = {round_val(a)}")
print(f"Adjacent side = {round_val(b)}")
print(f"Hypotenuse = {round_val(c)}")
print(f"Angle 1 = {round_val(angle)}°")
print(f"Angle 2 = {round_val(other_angle)}°")
else:
print("Invalid choice.")
line()
# -----------------------------------
# Special Angle Table
# -----------------------------------
def show_special_angles():
line()
print("Special Angle Table")
print(f"{'Angle':<10}{'sin':<15}{'cos':<15}{'tan':<15}")
line()
angles = [0, 30, 45, 60, 90, 120, 135, 150, 180, 270, 360]
for deg in angles:
rad = [Link](deg)
s = round_val([Link](rad))
c = round_val([Link](rad))
if abs([Link](rad)) < 1e-10:
t = "undefined"
else:
t = str(round_val([Link](rad)))
print(f"{str(deg)+'°':<10}{str(s):<15}{str(c):<15}{str(t):<15}")
line()
# -----------------------------------
# Identity Checker
# -----------------------------------
def identity_checker():
line()
print("Identity Checker")
print("Checks whether sin²x + cos²x = 1 for a given angle.")
deg = safe_float_input("Enter angle in degrees: ")
rad = [Link](deg)
lhs = [Link](rad)**2 + [Link](rad)**2
rhs = 1
print(f"LHS = sin²({deg}) + cos²({deg}) = {round_val(lhs)}")
print(f"RHS = {rhs}")
if abs(lhs - rhs) < 1e-9:
print("Identity verified.")
else:
print("Identity failed (likely due to floating-point rounding).")
line()
# -----------------------------------
# Quiz Generator
# -----------------------------------
def generate_trig_question():
qtype = [Link](["sin", "cos", "tan"])
# safer set for tan
if qtype == "tan":
angle = [Link]([0, 30, 45, 60, 120, 135, 150, 180])
else:
angle = [Link]([0, 30, 45, 60, 90, 120, 135, 150, 180])
rad = [Link](angle)
if qtype == "sin":
answer = round([Link](rad), 3)
elif qtype == "cos":
answer = round([Link](rad), 3)
else:
answer = round([Link](rad), 3)
return qtype, angle, answer
def trig_quiz():
line()
print("Trigonometry Quiz")
total = safe_int_input("How many questions? ")
score = 0
start_time = [Link]()
for i in range(1, total + 1):
qtype, angle, answer = generate_trig_question()
print(f"\nQuestion {i}:")
print(f"What is {qtype}({angle}°)?")
print("Round your answer to 3 decimal places.")
user = safe_float_input("Your answer: ")
if abs(user - answer) < 0.01:
print("Correct!")
score += 1
else:
print(f"Wrong. Correct answer = {answer}")
elapsed = [Link]() - start_time
line()
print(f"Final Score: {score}/{total}")
print(f"Time taken: {round_val(elapsed)} seconds")
line()
# -----------------------------------
# Menu
# -----------------------------------
def main_menu():
while True:
print("\nTrigonometry Mastery")
line()
print("1. Convert degrees to radians")
print("2. Convert radians to degrees")
print("3. Calculate sin, cos, tan")
print("4. Inverse trigonometric functions")
print("5. Solve a right triangle")
print("6. Show special angle table")
print("7. Check trig identity")
print("8. Trigonometry quiz")
print("9. Exit")
line()
choice = input("Choose an option: ").strip()
if choice == "1":
degrees_to_radians()
elif choice == "2":
radians_to_degrees()
elif choice == "3":
trig_values()
elif choice == "4":
inverse_trig()
elif choice == "5":
solve_right_triangle()
elif choice == "6":
show_special_angles()
elif choice == "7":
identity_checker()
elif choice == "8":
trig_quiz()
elif choice == "9":
print("Goodbye.")
break
else:
print("Invalid choice. Try again.")
if __name__ == "__main__":
main_menu()
Python code