func.py 746 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from typing import Callable
  2. def calculate(x:float, y:float, operation:str='a') -> None:
  3. def add():
  4. print(x + y)
  5. def sub():
  6. print(x - y)
  7. def div():
  8. if y == 0:
  9. print("На ноль делить нельзя!")
  10. else:
  11. print(x/y)
  12. def mul():
  13. print(x*y)
  14. match operation:
  15. case "a":
  16. add()
  17. case "s":
  18. sub()
  19. case "d":
  20. div()
  21. case "m":
  22. mul()
  23. case _:
  24. print("Ошибка. Данной операции не существует")
  25. def get_math_func(operation: str) -> Callable[[int, int], int]:
  26. def add(a: int, b: int) -> int:
  27. return a + b
  28. def subtract(a: int, b: int) -> int:
  29. return a - b
  30. if operation == "+":
  31. return add
  32. elif operation == "-":
  33. return subtract