Advertisement
simeonshopov

Operate

Jun 3rd, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. #!usr/local/bin/python3.8
  2. # -*- coding: utf-8 -*import
  3. OPERATORS = ('+', '-', '*', '/')
  4.  
  5.  
  6. def plus(*args):
  7.     return sum(args)
  8.  
  9.  
  10. def minus(*args):
  11.     args = list(args)
  12.     result = 0
  13.     for x in args:
  14.         result -= x
  15.     return result
  16.  
  17.  
  18. def multiply(*args):
  19.     args = list(args)
  20.     result = 1
  21.     for i in args:
  22.         result *= i
  23.     return result
  24.  
  25.  
  26. def div(*args):
  27.     args = list(args)
  28.     if 0 not in args:
  29.         result = 1
  30.         for i in args:
  31.             result /= i
  32.         return result
  33.     else:
  34.         return 0
  35.  
  36.  
  37. operations = {
  38.         '+': plus,
  39.         '-': minus,
  40.         '*': multiply,
  41.         '/': div,
  42.     }
  43.  
  44.  
  45. def operate(*args):
  46.     operator = args[0]
  47.     args = args[1:]
  48.     if operator in OPERATORS:
  49.         if args:
  50.             result = operations[operator](*args)
  51.             return result
  52.         else:
  53.             return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement