Advertisement
Guest User

calc with exit

a guest
Jan 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. def add(x, y):
  2.     """Эта функция складывает 2 числа"""
  3.  
  4.     return x + y
  5.  
  6. def subtract(x, y):
  7.     """Эта функция вычитает второе число из первого"""
  8.  
  9.     return x - y
  10.  
  11. def multiply(x, y):
  12.     """Эта функция перемножает 2 числа"""
  13.  
  14.     return x * y
  15.  
  16.  
  17. def divide(x, y):
  18.     """Эта функция делит первое число на второе"""
  19.  
  20.     return x / y
  21.  
  22.  
  23. def raisedtopower(x, y):
  24.     """Эта функция возводит число в степень"""
  25.  
  26.     return x ** y
  27.  
  28. # запрашивает выбор пользователя
  29. print("Select operation.")
  30. print("1.Add")
  31. print("2.Subtract")
  32. print("3.Multiply")
  33. print("4.Divide")
  34. print("5.Raised to power")
  35. print("6.Exit")
  36.  
  37. running = True
  38. while running:
  39.     choice = input("Enter choice(1/2/3/4/5/exit):")
  40.  
  41.     if choice == 'exit':
  42.         break
  43.  
  44.     num1 = int(input("Enter first number: "))
  45.     num2 = int(input("Enter second number: "))
  46.  
  47.     if choice == '1':
  48.         print(num1,"+",num2,"=", add(num1,num2))
  49.     elif choice == '2':
  50.         print(num1,"-",num2,"=", subtract(num1,num2))
  51.     elif choice == '3':
  52.         print(num1,"*",num2,"=", multiply(num1,num2))
  53.     elif choice == '4':
  54.         print(num1,"/",num2,"=", divide(num1,num2))
  55.     elif choice == '5':
  56.         print(num1, "**",num2,"=", raisedtopower(num1,num2))
  57.     else:
  58.         print("Invalid input")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement