Last active
February 1, 2016 08:52
-
-
Save otykhonruk/e7468c466b3669184c9e to your computer and use it in GitHub Desktop.
Simple calculator that can add, subtract, multiply and divide
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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