Advertisement
Spocoman

03. Calculations

Jan 25th, 2022
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. command = input()
  2. num1 = int(input())
  3. num2 = int(input())
  4.  
  5. def calculation():
  6.     if command == "multiply":
  7.         return num1 * num2
  8.     elif command == "divide":
  9.         if num1 != 0:
  10.             return num1 / num2
  11.     elif command == "add":
  12.         return num1 + num2
  13.     elif command == "subtract":
  14.         return num1 - num2
  15.  
  16. print(int(calculation()))
  17.  
  18.  
  19. Или с вложени функции:
  20.  
  21. command = input()
  22. num1 = int(input())
  23. num2 = int(input())
  24.  
  25.  
  26. def calculation():
  27.     def multiply():
  28.         return num1 * num2
  29.  
  30.     def divide():
  31.         if num1 != 0:
  32.             return num1 / num2
  33.  
  34.     def add():
  35.         return num1 + num2
  36.  
  37.     def subtract():
  38.         return num1 - num2
  39.  
  40.     return {"multiply": multiply(), "divide": divide(), "add": add(), "subtract": subtract()}[command]
  41.  
  42.  
  43. print(int(calculation()))
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement