Created
October 17, 2013 23:52
-
-
Save rezkam/7034349 to your computer and use it in GitHub Desktop.
Simple Calculator for Tehlug
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
def add(x, y): | |
return x + y | |
def sub(x, y): | |
return x - y | |
def mul(x, y): | |
return x * y | |
def div(x, y): | |
return x / y | |
def operation(sign): | |
if sign == '+': | |
return add | |
elif sign == '-': | |
return sub | |
elif sign == '*': | |
return mul | |
elif sign == '/': | |
return div | |
else: | |
return None | |
def calculate(x, func, y): | |
try: | |
return func(x, y) | |
except (NameError, TypeError) : | |
return 'func name is Wrong!use add, sub, mul and div' | |
except ZeroDivisionError: | |
return 'Division By Zero Is Not Allowed!!!' | |
except Exception: | |
return 'Unknown Error!' | |
import re | |
while True: | |
user = raw_input() | |
match = re.search(r'(\s*\d+\s*)([/+*-])(\s*\d+\s*)' , user) | |
if match: | |
op = operation(match.group(2)) | |
if op: | |
print calculate(float(match.group(1)), op, float(match.group(3))) | |
else: | |
print 'Please Enter Correct Input! number1 Operation number2 (Operations + - * / )' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment