CLI Calculator V1.5 09-03-2015, 09:19 PM
#1
Back when I was learning basic Ruby, I tried making a calculator. And failed. Miserably.
Today, I thought I'd go back and re-make it in Python and throw a CLI in with it.
Features:
Available operations:
Source:
Commands (other than equations):
Today, I thought I'd go back and re-make it in Python and throw a CLI in with it.
Features:
Code:
- CLI (duh)
- In-program help function
- Exception catching
- Fibonacci reference
- Formatted times tables
- Input history
- Exit functionAvailable operations:
Code:
+ addition
- subtraction
/ division
* multiplication
** exponents
./ floor division w/ remainderSource:
Code:
from sys import exit as e
import operator as op
def rem(a,b):
return "v: %s\n r: %s" % (a // b,a % b)
ops = {'+': op.add,'-': op.sub,'*': op.mul,'/': op.div,'**': op.pow,'%': op.mod,'./': rem}
def fib_to(n):
fibs = [1, 1]
for i in range(2, int(n)+1):
fibs.append(fibs[-1] + fibs[-2])
print", ".join(map(str, fibs))
def tables():
for row in range(1,13):
s = ''
for col in range(1,13):
s += '{:3} '.format(row * col)
print(s)
def interface():
m = raw_input("} ").split()
try:
if m == ["/?"]: print "/? Help\n/e Exit\n/f <n> Fibonacci series to <n>\n/x Multiplication tables"
elif m == ["/e"]: e()
elif "/f" in m: fib_to(m[1])
elif m == ["/x"]: tables()
else: print">>",str(ops[m[1]](float(m[0]),float(m[2])))
except IndexError:
print "Unknown command '%s'" % str(m[0])
except KeyError:
print "Invalid syntax: "+" ".join(m)
interface()
print "Syntax(); CLI Calculator:\n/? for help"
interface()Commands (other than equations):
Code:
/? Help
/e Exit
/f <n> Fibonacci series to <n>
/x Multiplication tablesIt's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.
don't have much in the first place, who feel the new currents and ride them the farthest.















![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)