Advertisement
eudemonics

python tip calculator

Dec 18th, 2013
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. import re, sys
  2.  
  3. def getTip():
  4.  
  5.    while True:
  6.  
  7.       billamount = raw_input("Please enter bill amount in decimal format --> ")
  8.  
  9.       try:
  10.          mybill = float(billamount)
  11.          break
  12.  
  13.       except:
  14.          pass
  15.          e = sys.exc_info()[0]
  16.          print("ERROR: %s" % e )
  17.          print("***Amount entered is not a valid decimal format***")
  18.  
  19.    while True:
  20.  
  21.       tippercent = raw_input("Please enter percentage of tip amount in as an integer --> ")
  22.  
  23.       try:
  24.          mytip = int(tippercent)
  25.          break
  26.  
  27.       except:
  28.          pass
  29.          e = sys.exc_info()[0]
  30.          print( "Error: %s" % e )
  31.          print("***Tip percentage not entered in valid round number format.***")
  32.  
  33.  
  34.    tipdecimal = mytip * 0.01
  35.  
  36.    tipamount = mybill * tipdecimal
  37.  
  38.    total = mybill + tipamount
  39.  
  40.    print("Your " + str(tippercent) + "% tip amount for bill of " + str(mybill) + " is: {0:0.2f}".format(tipamount))
  41.  
  42.    print("Your total bill is: {0:0.2f}".format(total))
  43.  
  44.    newtip = 0
  45.  
  46. getTip()
  47.  
  48. while True:
  49.  
  50.    newtip = raw_input("Would you like to calculate another tip? Y/N --> ")
  51.  
  52.    try:
  53.       matchY = re.search(r'(?i)Y', newtip)
  54.       matchN = re.search(r'(?i)N', newtip)
  55.  
  56.       if matchY:
  57.          getTip()
  58.  
  59.       if matchN:
  60.          print("Goodbye!")
  61.          sys.exit()
  62.  
  63.       else:
  64.          pass
  65.  
  66.    except:
  67.       pass
  68.       sys.exit()
  69.  
  70. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement