Skip to content

Instantly share code, notes, and snippets.

@otykhonruk
Last active February 1, 2016 08:52
Show Gist options
  • Save otykhonruk/e7468c466b3669184c9e to your computer and use it in GitHub Desktop.
Save otykhonruk/e7468c466b3669184c9e to your computer and use it in GitHub Desktop.
Simple calculator that can add, subtract, multiply and divide
import operator as op
# Simple calculator that can add, subtract, multiply and divide
OPS = (("Add", "+", op.iadd),
("Subtract", "-", op.isub),
("Multiply", "*", op.imul),
("Divide", "/", op.truediv))
valid_choices = list(map(str, range(1, len(OPS)+1)))
# take input from the user
print("Select operation.")
for i, o in zip(valid_choices, OPS):
print("{}. {}".format(i, o[0]))
choice = input("Enter choice(" + "/".join(valid_choices) + "):")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice in valid_choices:
_, sym, fn = OPS[int(choice)-1]
print("{} {} {} = {}".format(num1, sym, num2, fn(num1, num2)))
else:
print("Invalid input")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment