-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
95 lines (82 loc) · 3.46 KB
/
calculator.py
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
94
95
import math
def calculator():
print("Welcome to the Scientific Calculator")
print("Select an operation:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Sine (sin)")
print("6. Cosine (cos)")
print("7. Tangent (tan)")
print("8. Square Root (sqrt)")
print("9. Power (^)")
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b != 0:
return a / b
else:
return "Error: Division by zero is not allowed."
def sine(x):
return math.sin(math.radians(x))
def cosine(x):
return math.cos(math.radians(x))
def tangent(x):
return math.tan(math.radians(x))
def square_root(x):
if x >= 0:
return math.sqrt(x)
else:
return "Error: Square root of a negative number is not allowed."
def power(base, exponent):
return math.pow(base, exponent)
while True:
choice = input("Enter the number of the operation you'd like to perform (or 'q' to quit): ")
if choice.lower() == 'q':
print("Exiting the calculator. Goodbye!")
break
try:
match choice:
case '1':
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
print(f"Result: {num1} + {num2} = {add(num1, num2)}")
case '2':
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
print(f"Result: {num1} - {num2} = {subtract(num1, num2)}")
case '3':
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
print(f"Result: {num1} * {num2} = {multiply(num1, num2)}")
case '4':
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
print(f"Result: {num1} / {num2} = {divide(num1, num2)}")
case '5':
num = float(input("Enter the number (in degrees): "))
print(f"Result: sin({num}) = {sine(num)}")
case '6':
num = float(input("Enter the number (in degrees): "))
print(f"Result: cos({num}) = {cosine(num)}")
case '7':
num = float(input("Enter the number (in degrees): "))
print(f"Result: tan({num}) = {tangent(num)}")
case '8':
num = float(input("Enter the number: "))
print(f"Result: sqrt({num}) = {square_root(num)}")
case '9':
base = float(input("Enter the base number: "))
exponent = float(input("Enter the exponent: "))
print(f"Result: {base}^{exponent} = {power(base, exponent)}")
case _:
print("Invalid choice. Please select a valid operation.")
except ValueError:
print("Error: Invalid input. Please enter numeric values.")
if __name__ == "__main__":
calculator()