Last active
June 26, 2016 09:11
-
-
Save uroybd/608421f0e1dd75962034a1cabdeb36a3 to your computer and use it in GitHub Desktop.
Calculator(Shortend from a code of a fellow member from pycharmers group)
This file contains 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 math | |
def get_num(): | |
inp = input("Enter a number: ") | |
try: | |
return float(inp) | |
except: | |
print("Enter a valid number.") | |
get_num() | |
operation_dispatcher = { | |
'add': lambda: get_num() + get_num(), | |
'+': lambda: get_num() + get_num(), | |
'subtract': lambda: get_num() - get_num(), | |
'-': lambda: get_num() - get_num(), | |
'multiply': lambda: get_num() * get_num(), | |
'*': lambda: get_num() * get_num(), | |
'divide': lambda: get_num() / get_num(), | |
'/': lambda: get_num() / get_num(), | |
'exponent': lambda: get_num() ** get_num(), | |
'sqrt': lambda: math.sqrt(get_num()), | |
'absolute': lambda: math.absolute(get_num()), | |
'fac': lambda: math.factorial(get_num()), | |
'roundoff': lambda: int(float(get_num())), | |
'round': lambda: int(round(get_num())) | |
} | |
print("Welcome to calculator!") | |
while True: | |
inp = input("Enter an operation: ").lower() | |
if inp == "exit": | |
print("Bye.") | |
break | |
else: | |
try: | |
print(operation_dispatcher[inp]()) | |
except: | |
print("Operation not recognized.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment