Advertisement
Guest User

Calculator_trial.py

a guest
Jun 24th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. #operations
  2. def a(x,y):
  3.     return x + y    
  4.    
  5. def m(x,y):
  6.     return x * y
  7.    
  8. def s(x,y):
  9.     return x - y
  10.    
  11. def d(x,y):
  12.     try:
  13.         return x / y
  14.     except ZeroDivisionError:
  15.         print ("Unable to divide by 0. Try another number.")
  16.  
  17. while True:
  18.     try:
  19.         x = float(input("Enter a number-->\n"))
  20.         y = float(input("Enter another number-->\n"))
  21.        
  22.     except (ValueError, NameError):
  23.         print ("Invalid input. Please try again.")
  24.         continue
  25.  
  26.     try:
  27.         o = input("Select an Operation-->  (+) (*) (-) (/)\n")
  28.        
  29.     except (ValueError, NameError):
  30.         print ("Invalid Operation. Please try again. (+ or * or - or /)")
  31.         continue
  32.  
  33.     if o == "+":
  34.         ans = a(x,y)
  35.     elif o == "*":
  36.         ans = m(x,y)
  37.     elif o == "-":
  38.         ans = s(x,y)
  39.     elif o == "/":
  40.         ans = d(x,y)
  41.    
  42.     if y != 0:
  43.         print ("Your answer is:")
  44.         print ("x" , o , "y" , "=" , ans , sep=" ")
  45.     else:
  46.         continue
  47.        
  48.     while True:
  49.         restart = input ("Run again? (y / n)")
  50.         if restart == 'y':
  51.             break
  52.         elif restart != "n":
  53.             print ("Invalid input. Type (y / n)")
  54.             continue
  55.         else:
  56.             print ("Thanks for using my calculator!")
  57.             quit()
  58.     else:
  59.         print("An unexpected error has occured. Terminating...")
  60.         quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement