-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
62 lines (53 loc) · 2.48 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
import arithmetic
def do_math():
while True:
user_input = raw_input("> ").strip()
if user_input == "q":
break
# separate user input
input_list = user_input.split()
# determine math function to be called and check for errors
if len(input_list) == 1 or len(input_list) == 2:
if input_list[0] in ["+", "-", "*", "/", "pow", "mod"]:
print "Oops! You need to give us more numbers! Try again."
elif len(input_list) == 1 and input_list[0] in ["square", "cube"]:
print "Oops! You need to give us 1 number! Try again."
elif len(input_list) == 2 and input_list[0] in ["square", "cube"]:
# convert user input values to integers for math
second = int(input_list[1])
if input_list[0] == "square":
print arithmetic.square(second)
elif input_list[0] == "cube":
print arithmetic.cube(second)
else:
print "I don't understand what math operation you want me to do. Try again."
elif len(input_list) >= 3:
# convert user input values to integers for math
second, third = int(input_list[1]), int(input_list[2])
if input_list[0] == "+":
print arithmetic.add(input_list[1:])
elif input_list[0] == "-":
print arithmetic.subtract(input_list[1:])
elif input_list[0] == "*":
print arithmetic.multiply(input_list[1:])
elif input_list[0] == "/":
print arithmetic.divide(input_list[1:])
elif input_list[0] in ["square", "cube"]:
print "Oops! You gave us too many numbers! Just give 1. Try again."
elif input_list[0] in ["pow", "mod"]:
# error check for too many numbers
if len(input_list) == 3:
if input_list[0] == "pow":
print arithmetic.power(second, third)
elif input_list[0] == "mod":
print arithmetic.mod(second, third)
else:
print "Oops! You gave us too many numbers! Just give 2. Try again."
else:
print "I don't understand what kind of math you want me to do."
else:
print "I don't understand. You said too much. Talk less."
def main():
do_math()
if __name__ == '__main__':
main()