Skip to content

Instantly share code, notes, and snippets.

@joelibaceta
Created June 28, 2019 17:44
Show Gist options
  • Save joelibaceta/c97bb681abe3dd06b1f08729b418f332 to your computer and use it in GitHub Desktop.
Save joelibaceta/c97bb681abe3dd06b1f08729b418f332 to your computer and use it in GitHub Desktop.
import re
print("\n\033[38;1mCalcular +, , *, / de 2 números:\033[0m\n")
def solve(str_operation):
pattern = re.compile("([0-9]+)[\s]*([\+\-\/\*])[\s]*([0-9]+)")
match = pattern.match(str_operation)
(operator1, operation, operator2) = match.groups()
if operation == "+":
return int(operator1) + int(operator2)
elif operation == "-":
return int(operator1) - int(operator2)
elif operation == "/":
return int(operator1) / int(operator2)
elif operation == "*":
return int(operator1) * int(operator2)
else:
return None;
# Loop Infinito
while True:
operation = input("Ingerese una oparacion o q para salir: ")
if operation == "q":
break;
else:
result = solve(operation)
if result == None:
print ("Operacion no permitida\n")
else:
print("\033[38;1m" + str(result) + "\033[0m\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment