-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
51 lines (41 loc) · 1.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
"""
calculator.py
Using our arithmetic.py file from Exercise02, create the
calculator program yourself in this file.
"""
from arithmetic import *
def calculator():
entered_string = None
while (entered_string != "q"):
entered_string = raw_input(">> ")
user_input = entered_string.split(" ")
try:
operation = user_input[0]
operand1 = float(user_input[1])
except (IndexError, ValueError):
if (operation != "q"):
print "Invalid input. Check that your operator and operands are correct, and try again.\n"
continue
if len(user_input) == 3:
operand2 = float(user_input[2])
if operation == "+":
result = add(operand1, operand2)
elif operation == "-":
result = subtract(operand1, operand2)
elif operation == "*":
result = multiply(operand1, operand2)
elif operation == "/":
result = divide(operand1, operand2)
elif operation == "square":
result = square(operand1)
elif operation == "cube":
result = cube(operand1)
elif operation == "power":
result = power(operand1, operand2)
elif operation == "mod":
result = mod(operand1, operand2)
else:
entered_string = raw_input("I don't understand. Please try again.\n>> ")
print "The result is: {:.2f}.".format(result)
return
calculator()