-
Notifications
You must be signed in to change notification settings - Fork 601
/
calc.py
executable file
·81 lines (65 loc) · 1.82 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
#!/usr/bin/env python
# Copyright 2016 -- Levi Starrett & Jay Hankins
# for educational purposes only
#
# For use in CS 190: https://github.com/Purdue-CSUSB/CS-190-F2016/
#
# 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
# -------------------------------------------------------- #
# -------------------------------------------------------- #
# -- 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)
else:
print "Invalid operation..."
q = raw_input("Quit? [y/n] ")
if (q == "y" or q == "Y"):
break
# -------------------------------------------------------- #