diff options
| -rw-r--r-- | README | 1 | ||||
| -rw-r--r-- | main.py | 47 |
2 files changed, 48 insertions, 0 deletions
@@ -0,0 +1 @@ +It's a simple calculator written in python for school. It can only do operations between two numbers. @@ -0,0 +1,47 @@ +ops = "+-*/^" + +def calc(a, b, op): + if op == '+': + return a + b + elif op == '-': + return a - b + elif op == '*': + return a * b + elif op == '/': + return a / b + elif op == '^': + return a ** b + +help_menu = "'exit' - quit\n'help' - print this menu\nInput two numbers and an operator separated by spaces.\nExample input: A + B\nAvailable operators: + - * / ^" +print(help_menu) + +while True: + instr = input("> ") + if instr == "exit": + break + elif instr == "help": + print(help_menu) + continue + args = instr.split() + + if len(args) < 3: + print("Not enough arguments to perform operation.") + continue + + try: + args[0] = float(args[0]) + except: + print("Invalid first argument.") + continue + + try: + args[2] = float(args[2]) + except: + print("Invalid second argument.") + continue + + if args[1] in ops: + print(calc(args[0], args[2], args[1])) + else: + print("Invalid operation.") + |
