This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
76 lines (51 loc) · 1.78 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
"""A prefix-notation calculator."""
from arithmetic import *
while True:
user_input = input("> ")
tokens = user_input.split(" ")
if "q" in tokens:
print("You will exit.")
break
elif len(tokens) < 2:
print("Not enough inputs.")
continue
operator = tokens[0]
num1 = tokens[1]
if len(tokens) < 3:
num2 = "0"
else:
num2 = tokens[2]
if len(tokens) > 3:
num3 = tokens[3]
# A place to store the return value of the math function we call,
# to give us one clear place where that result is printed.
result = None
if not num1.isdigit() or not num2.isdigit():
print("Those aren't numbers!")
continue
# We have to cast each value we pass to an arithmetic function from a
# a string into a numeric type. If we use float across the board, all
# results will have decimal points, so let's do that for consistency.
elif operator == "+":
result = add(float(num1), float(num2))
elif operator == "-":
result = subtract(float(num1), float(num2))
elif operator == "*":
result = multiply(float(num1), float(num2))
elif operator == "/":
result = divide(float(num1), float(num2))
elif operator == "square":
result = square(float(num1))
elif operator == "cube":
result = cube(float(num1))
elif operator == "pow":
result = power(float(num1), float(num2))
elif operator == "mod":
result = mod(float(num1), float(num2))
elif operator == "x+":
result = add_mult(float(num1), float(num2), float(num3))
elif operator == "cubes+":
result = add_cubes(float(num1), float(num2))
else:
result = "Please enter an operator followed by two integers."
print(result)