-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcal_up.py
More file actions
66 lines (60 loc) · 1.95 KB
/
Copy pathcal_up.py
File metadata and controls
66 lines (60 loc) · 1.95 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
import numpy as np
def calculator():
print("insert values to calculate\n# = square root\n$ for = square\n^ = power\n+ = addition\n- = subtraction\n* = multiplication\n/ = division"
)
x = input()
xseperated = ""
y = []
for j in x:
if j.isdigit() or j == ".":
xseperated += j
else:
if xseperated != "":
y.append(float(xseperated))
xseperated = ""
y.append(j)
if xseperated != "":
y.append(float(xseperated))
# print(y)
# print(len(y))
result = y[0]
i = 1
while i < len(y):
op = y[i]
if op == "#":
if result <= 0:
result = "error: cannot take square root of negative number"
break
result = np.sqrt(result)
i += 1
elif op == "$":
result = result * result
i += 1
# elif op == "!":
#CONTINUE FROM HERE TO INTEGRATE FACTORIAL CALCULATIONS AND FIBONACCI CALCULATIONS
else:
next_num = y[i + 1]
if op == "+":
result = result + next_num
elif op == "-":
result = result - next_num
elif op == "*":
result = result * next_num
elif op == "^":
result = result ** next_num
elif op == "/":
if next_num == 0:
result = "cannot divide by zero"
break
result = result / next_num
i += 2
print(result, "\n")
recalculate = input("Do you want to perform another calculation? (yes/no): ")
while recalculate.lower() == "yes":
calculator()
if recalculate.lower() == "no":
print("Thank you for using the calculator!")
else:
if recalculate.lower() != "no" and recalculate.lower() != "yes":
print("Invalid input. Exiting the calculator.")
calculator()