0% found this document useful (0 votes)
3 views

calculation

The document contains a Python script for calculating the cosine or sine of a user-provided number. It includes functions for input validation, choice selection, and calculation of trigonometric values, returning results as fractions. The script is designed to run interactively in a console environment.

Uploaded by

baoken980
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

calculation

The document contains a Python script for calculating the cosine or sine of a user-provided number. It includes functions for input validation, choice selection, and calculation of trigonometric values, returning results as fractions. The script is designed to run interactively in a console environment.

Uploaded by

baoken980
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1/29/25, 9:30 PM calculation.

py

python/calculation.py

1 '''
2 Jack Nguyen
3 IST
4 27 Jan 25
5 Python Calculation
6 '''
7 import math
8 from fractions import Fraction
9
10 def get_number():
11 while True:
12 try:
13 number = float(input("Enter a number: "))
14 return number
15 except ValueError:
16 print("Invalid input. Please enter a valid number.")
17
18 def get_choice():
19 while True:
20 choice = input("Do you want the 'cos' or 'sin' value of the number? ").strip().lower()
21 if choice in ["cos", "sin"]:
22 return choice
23 else:
24 print("Invalid choice. Please enter 'cos' or 'sin'.")
25
26 def calculate_trig_value­
(number, choice):
27 if choice == "cos":
28 return math.cos(number)
29 elif choice == "sin":
30 return math.sin(number)
31
32 def main():
33 number = get_number()
34 choice = get_choice()
35 trig_value = calculate_trig_value­
(number, choice)
36 fractional_value = Fraction(trig_value).limit_denominator()
37 print(f"The {choice} value of {number} is {fractional_value}")
38
39 if __name__ == "__main__":
40 main()
41

127.0.0.1:36337/9d8c03b0-bd67-4072-9b68-9c5307c572e3/ 1/1

You might also like