forked from y8tireu/My-Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.py
102 lines (87 loc) · 2.55 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
96
97
98
99
100
101
102
import math
def add(x, y):
return x + y
def sub(x, y):
return x - y
def mul(x, y):
return x * y
def div(x, y):
if y == 0:
raise ValueError("You made An Amuku Damku Amuku Dumal Mistake")
return x / y
def pow(x, y):
return x ** y
def sqrt(x):
return math.sqrt(x)
def sin(x):
return math.sin(math.radians(x))
def cos(x):
return math.cos(math.radians(x))
def tan(x):
return math.tan(math.radians(x))
def log(x):
return math.log(x)
def exp(x):
return math.exp(x)
def factorial(x):
if x < 0:
raise ValueError("Factorial is not defined for negative numbers!")
result = 1
for i in range(1, int(x) + 1):
result *= i
return result
def main():
print("Welcome to the Attam Pota Scientific Calculator!")
print("Available functions:")
print(" +: addition")
print(" -: subtraction")
print(" *: multiplication")
print(" /: division")
print(" ^: exponentiation")
print(" sqrt: square root")
print(" sin: sine")
print(" cos: cosine")
print(" tan: tangent")
print(" log: natural logarithm")
print(" exp: exponential function")
print(" !: factorial")
print(" quit: exit the calculator")
while True:
user_input = input("Enter a function and arguments (e.g. '+ 2 3'): ")
if user_input.lower() == "quit":
break
try:
func, *args = user_input.split()
args = [float(arg) for arg in args]
if func == "+":
result = add(args[0], args[1])
elif func == "-":
result = sub(args[0], args[1])
elif func == "*":
result = mul(args[0], args[1])
elif func == "/":
result = div(args[0], args[1])
elif func == "^":
result = pow(args[0], args[1])
elif func == "sqrt":
result = sqrt(args[0])
elif func == "sin":
result = sin(args[0])
elif func == "cos":
result = cos(args[0])
elif func == "tan":
result = tan(args[0])
elif func == "log":
result = log(args[0])
elif func == "exp":
result = exp(args[0])
elif func == "!":
result = factorial(args[0])
else:
print("Invalid function!")
continue
print(f"Result: {result:.4f}")
except ValueError as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()