Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import operator
- def operacion(x, y, op):
- operadores = {
- '+': operator.add,
- '-': operator.sub,
- '*': operator.mul,
- '/': operator.truediv,
- '=': operator.eq
- }
- fop = operadores.get(op)
- if fop is not None:
- print(fop(x, y))
- else:
- raise ValueError("Operador no valido")
- >>> operacion(5, 4, "+")
- 9
- >>> operacion(5, 4, "-")
- 1
- >>> operacion(5, 4, "*")
- 20
- >>> operacion(5, 4, "/")
- 1.25
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement