![]() |
|
CLI Calculator V1.5 - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: CLI Calculator V1.5 (/Thread-CLI-Calculator-V1-5) |
CLI Calculator V1.5 - Inori - 09-03-2015 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: 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 tablesRE: CLI Calculator V1.5 - Inori - 09-07-2015 Updated as of 2015-09-06. Changes: Code: - added float returns
- added floor division /w returned remainder
- added history documentation (function was available in 1.0-1.4)
- changed help command (/h --> /?)
- cleaned up codeRE: CLI Calculator V1.5 - mugen - 09-07-2015 Sorry if this comes off as rude, but unless you're just trying to improve (even then, making calculators won't help you much), what's the point of this? I could drop into a Python interpreter and it'd be almost no different from your script, with the exception that you added a couple of small things to yours. Also, yours unnecessarily changes the result to a float even when you're just operating with 2 integers. RE: CLI Calculator V1.5 - Inori - 09-07-2015 (09-07-2015, 03:00 AM)mugen Wrote: Sorry if this comes off as rude, but unless you're just trying to improve (even then, making calculators won't help you much), what's the point of this? I could drop into a Python interpreter and it'd be almost no different from your script, with the exception that you added a couple of small things to yours. Also, yours unnecessarily changes the result to a float even when you're just operating with 2 integers. By making this, I'm improving my skills marginally by adding certain features and using functions I'm not too familiar with; giving people a compact CLI with some good features and easy syntax is cool, instead of the shitty "which operation do you want to do?" type. And yes, you could use IDLE for the purpose of this, but you wouldn't have access to some of the tools I've included, nor my modified floor division. On the topic of the float return, I ran into a bug wherein I couldn't use the calculator when the input wasn't an integer, so I resorted to using floats. I may change this in a later update. |