-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
85 lines (71 loc) · 2.55 KB
/
Copy pathcalculator.py
File metadata and controls
85 lines (71 loc) · 2.55 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import string
from arithmetic import *
while True:
user_input = raw_input("> ")
tokens = string.split(user_input)
if tokens[0] == "+":
if len(tokens) < 3:
print "Please enter at least two numbers to add. For example: + 1 2"
else:
tokens.pop(0)
tokensint = []
for i in range(len(tokens)):
tokensint.append(int(tokens[i]))
print add(*tokensint)
elif tokens[0] == "-":
if len(tokens) < 3:
print "Please enter at least two numbers to subtract. For example: - 3 1"
else:
tokens.pop(0)
tokensint = []
for i in range(len(tokens)):
tokensint.append(int(tokens[i]))
print subtract(*tokensint)
elif tokens[0] == "*":
if len(tokens) < 3:
print "Please enter at least two numbers to multiply. For example: * 4 2"
else:
tokens.pop(0)
tokensint = []
for i in range(len(tokens)):
tokensint.append(int(tokens[i]))
print multiply(*tokensint)
elif tokens[0] == "/":
if len(tokens) < 3:
print "Please enter at least two numbers to divide. For example: / 4 2"
else:
tokens.pop(0)
tokensint = []
for i in range(len(tokens)):
tokensint.append(float(tokens[i]))
print divide(*tokensint)
elif tokens[0] == "square":
if len(tokens) != 2:
print "Please enter one number to square. For example: square 3"
else:
tokens[1] = int(tokens[1])
print square(tokens[1])
elif tokens[0] == "cube":
if len(tokens) != 2:
print "Please enter one number to cube. For example: cube 2"
else:
tokens[1] = int(tokens[1])
print cube(tokens[1])
elif tokens[0] == "power":
if len(tokens) != 3:
print "Please enter exactly two numbers. For example: power 2 5"
else:
tokens[1] = int(tokens[1])
tokens[2] = int(tokens[2])
print power(tokens[1], tokens[2])
elif tokens[0] == "mod":
if len(tokens) != 3:
print "Please enter exactly two numbers. For example: mod 5 3"
else:
tokens[1] = int(tokens[1])
tokens[2] = int(tokens[2])
print mod(tokens[1], tokens[2])
elif tokens[0] == "q":
exit()
else:
print "Sorry, I'm not sure what that means. Please enter a valid argument."