-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculator.py
65 lines (54 loc) · 1.51 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
from math import sqrt
print("Dors Calculator")
print("Type 'exit' or press Control-C to quit.")
print("\n")
running = True
class EquationError(Exception):
pass
def get_equation_parts(array):
global number_1
global number_2
global operator
if len(array) == 1:
operator = array[0]
elif len(array) == 2:
operator = array[0]
number_1 = float(array[1])
elif len(array) == 3:
operator = array[1]
number_1 = float(array[0])
number_2 = float(array[2])
else:
raise EquationError
def process_equation():
equation = input("Enter an equation: ")
equation_parts = equation.split(" ")
return equation_parts
def calc():
global running
try:
get_equation_parts(process_equation())
if operator == "+":
answer = number_1 + number_2
if operator == "-":
answer = number_1 - number_2
if operator == "*":
answer = number_1 * number_2
if operator == "/":
answer = number_1 / number_2
if operator == "^":
answer = number_1 ** number_2
if operator.lower() == "sqrt":
answer = sqrt(number_1)
if operator.lower() == "mod":
answer = number_1 % number_2
if operator.lower() == "exit":
running = False
return answer
except KeyboardInterrupt:
running = False
except:
print("Invalid equation.")
if __name__ == "__main__":
while running == True:
calc()