-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.py
More file actions
93 lines (78 loc) · 2.63 KB
/
Calculator.py
File metadata and controls
93 lines (78 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import math
def show_menu():
print("\n" + "="*40)
print("Advanced Calculator")
print("="*40)
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Divisors of a number")
print("6. Sine (input in radians)")
print("7. Cosine (input in radians)")
print("0. Exit")
print("="*40)
def get_number(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print("Error: Please enter a valid number.")
def get_positive_int(prompt):
while True:
try:
num = int(input(prompt))
if num > 0:
return num
else:
print("Error: Please enter a positive integer.")
except ValueError:
print("Error: Input must be an integer.")
def divisors(n):
n = int(n)
divs = [i for i in range(1, n+1) if n % i == 0]
return divs
def main():
while True:
show_menu()
choice = input("Your choice: ")
if choice == '0':
print("Exiting program. Goodbye!")
break
elif choice == '1':
a = get_number("First number: ")
b = get_number("Second number: ")
print(f"Result: {a} + {b} = {a + b}")
elif choice == '2':
a = get_number("First number: ")
b = get_number("Second number: ")
print(f"Result: {a} - {b} = {a - b}")
elif choice == '3':
a = get_number("First number: ")
b = get_number("Second number: ")
print(f"Result: {a} * {b} = {a * b}")
elif choice == '4':
a = get_number("Dividend: ")
b = get_number("Divisor: ")
if b == 0:
print("Error: Division by zero is not allowed.")
else:
print(f"Result: {a} / {b} = {a / b}")
elif choice == '5':
num = get_positive_int("Enter a positive integer: ")
divs = divisors(num)
print(f"Divisors of {num}: {divs}")
elif choice == '6':
deg = get_number("Enter angle in degrees : ")
rad = math.radians(deg) # Convert degrees to radians
result = math.sin(rad)
print(f"sin({deg}) = {result}")
elif choice == '7':
deg = get_number("Enter angle in degrees : ")
rad = math.radians(deg) # Convert degrees to radians6
result = math.cos(rad)
print(f"cos({deg}) = {result}")
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()