Back to main page
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax <mahn.maxwell@gmail.com>2022-01-06 12:55:30 -0500
committerMax <mahn.maxwell@gmail.com>2022-01-06 12:55:30 -0500
commitd05616bb4f7e6983ef02317da5ccfcdd04d5a23e (patch)
tree66d27d4760e04280d1f5415df3aabe2a3d021765
initial commitHEADmain
-rw-r--r--README1
-rw-r--r--main.py47
2 files changed, 48 insertions, 0 deletions
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.")
+