crzcas

Basic calculator with try-except

Feb 7th, 2022
1,270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.60 KB | None | 0 0
  1. #Python program to make basic calculator
  2.  
  3. def addition(num1, num2):
  4.     return num1 + num2
  5.  
  6. def subtraction(num1, num2):
  7.     return num1 - num2
  8.  
  9. def multiplication(num1, num2):
  10.     return num1 * num2
  11.  
  12. def divide(num1, num2):
  13.     try:
  14.         num1 / num2
  15.     except ZeroDivisionError:
  16.         print("division by zero is not allowed, try again!")
  17.         print()
  18.     else:
  19.         print(value1, "/", value2, "=", value1 / value2)
  20.         print()
  21.     finally:
  22.         print("executing finally clause")
  23.         print()
  24.  
  25.  
  26. finished = False
  27. while finished == False:
  28.  
  29.     print("Hi, I am your personal calculator. I can help you with the following operations: ")
  30.  
  31.     print("Select a number:  1-Addition, 2-Subtraction, 3-Multiplication, 4-Division, 5-End")
  32.     operation = int(input("Choose  number of operation 1, 2, 3, 4, 5 : "))
  33.  
  34.  
  35.     if operation == 5:
  36.         print()
  37.         print("Have a good day, bye.")
  38.         break
  39.  
  40.  
  41.     if (operation >= 1) and (operation <=5):
  42.         pass
  43.     else:
  44.         print()
  45.         print("The number of the operation must be between 1 and 5.")
  46.         break
  47.  
  48.  
  49.     value1 = int(input("Enter 1st number: "))
  50.     value2 = int(input("Enter 2nd number: "))
  51.  
  52.  
  53.     if operation == 1:
  54.         print(value1, "+", value2, "=", addition(value1, value2))
  55.         print()
  56.  
  57.     elif operation == 2:
  58.         print(value1, "-", value2, "=", subtraction(value1, value2))
  59.         print()
  60.  
  61.     elif operation == 3:
  62.         print(value1, "*", value2, "=", multiplication(value1, value2))
  63.         print()
  64.  
  65.     elif operation == 4:
  66.         divide(value1, value2)
  67.  
Advertisement
Add Comment
Please, Sign In to add comment