From d05616bb4f7e6983ef02317da5ccfcdd04d5a23e Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 6 Jan 2022 12:55:30 -0500 Subject: initial commit --- README | 1 + main.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 README create mode 100644 main.py diff --git a/README b/README new file mode 100644 index 0000000..0d0992b --- /dev/null +++ b/README @@ -0,0 +1 @@ +It's a simple calculator written in python for school. It can only do operations between two numbers. diff --git a/main.py b/main.py new file mode 100644 index 0000000..dc99e14 --- /dev/null +++ b/main.py @@ -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.") + -- cgit v1.2.3