Commit d74cb056 authored by muris2016's avatar muris2016
Browse files

Clase Calculadora mejorada

parent e79f72d0
......@@ -2,9 +2,16 @@
# -*- coding:utf-8 -*-
import sys
import calc
class Calculadora():
class Calculadora:
def __init__(self):
self.operations = {"suma": self.sum, "resta": self.substraction}
def operating(self, calculator_operation, op1, op2):
return calculator_operation(op1, op2)
def sum(self, a, b):
return a + b
......@@ -12,33 +19,18 @@ class Calculadora():
return a - b
def to_number(num):
try:
if '.' in num:
return float(num)
else:
return int(num)
except:
sys.exit("Error")
def do_operation(operation, op1, op2):
calculator = Calculadora()
if operation == "suma":
result = calculator.sum(op1, op2)
print(result)
elif operation == "resta":
result = calculator.substraction(op1, op2)
print(result)
def do_operation(calculator, operation, op1, op2):
else:
print ("Not allowed operation", operation)
try:
return calculator.operating(calculator.operations[operation],
op1, op2)
except KeyError:
sys.exit("Not allowed operation " + operation)
if __name__ == "__main__":
op1 = to_number(sys.argv[1])
op1 = calc.to_number(sys.argv[1])
operation = sys.argv[2]
op2 = to_number(sys.argv[3])
do_operation(operation, op1, op2)
op2 = calc.to_number(sys.argv[3])
calculator = Calculadora()
print(do_operation(calculator, operation, op1, op2))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment