Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. import operator
  2.  
  3. def operacion(x, y, op):
  4. operadores = {
  5. '+': operator.add,
  6. '-': operator.sub,
  7. '*': operator.mul,
  8. '/': operator.truediv,
  9. '=': operator.eq
  10. }
  11. fop = operadores.get(op)
  12. if fop is not None:
  13. print(fop(x, y))
  14. else:
  15. raise ValueError("Operador no valido")
  16.  
  17. >>> operacion(5, 4, "+")
  18. 9
  19. >>> operacion(5, 4, "-")
  20. 1
  21. >>> operacion(5, 4, "*")
  22. 20
  23. >>> operacion(5, 4, "/")
  24. 1.25
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement