-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.py
92 lines (73 loc) · 1.96 KB
/
calc.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright 2014 -- Levi Starrett
# for educational purposes only
#
# Calculator -- a four function calculator commandline tool
import sys
# -------------------------------------------------------- #
# -- CALCULATOR FUNCTIONS -------------------------------- #
# -------------------------------------------------------- #
# Add function
# a -- addend
# b -- augend
def add(a, b):
return a + b
# Subtract function
# a -- minuend
# b -- subtrahend
def sub(a, b):
return a - b
# Multiply function
# a -- multiplicand
# b -- multiplier
def mult(a, b):
return a * b
# Divide function
# a -- dividend
# b -- divisor
def div(a, b):
return a / b
def exp(a, b):
return a ** b
# Modulus function
# a -- dividend
# b -- divisor
def mod(a, b):
return a % b
# -------------------------------------------------------- #
# -------------------------------------------------------- #
# -- MAIN FUNCTIONAILTY -- DO NOT EDIT ------------------- #
# -------------------------------------------------------- #
a = None
b = None
op = None
while (True):
# get input values
a = raw_input("Enter the first argument: ")
op = raw_input("Enter the operation: ")
b = raw_input("Enter the second argument: ")
try:
a = int(a)
b = int(b)
except ValueError:
print "Invalid number argument..."
op = None
# decide function
if (op != None):
if (op == "+"):
print "Sum: ", add(a, b)
elif (op == "-"):
print "Difference: ", sub(a, b)
elif (op == "*"):
print "Product: ", mult(a, b)
elif (op == "/"):
print "Quotient: ", div(a, b)
elif (op == "**"):
print "Product: ", exp(a, b)
elif (op == "%"):
print "Modulus: ", mod(a, b)
else:
print "Invalid operation..."
q = raw_input("Quit? [y/n] ")
if (q == "y" or q == "Y"):
break
# -------------------------------------------------------- #