furas

Python - Calculate tax (FB - LearnPython.org)

Sep 4th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. #
  2. # https://www.facebook.com/groups/learnpython.org/permalink/1181939875203961/
  3. #
  4.  
  5. def calculate_tax(people):
  6.  
  7.     for key, earning in people.items():
  8.        
  9.         if earning <= 1000:
  10.             total_tax = 0
  11.         elif earning <= 10000:
  12.             tax1 = 0 #* 1000
  13.             tax2 = 0.1 * (earning - 1000)
  14.             total_tax = tax1 + tax2
  15.         elif earning <= 20200:
  16.             tax1 = 0 #* 1000
  17.             tax2 = 0.1 *9000
  18.             tax3 = 0.15 * (earning - 10000)
  19.             total_tax = tax1+tax2+tax3
  20.         elif earning <= 30750:
  21.             tax1 = 0 #* 1000
  22.             tax2 = 0.1 * 9000
  23.             tax3 = 0.15 * 10200
  24.             tax4 = 0.20 * (earning - 20200)
  25.             total_tax = tax1+tax2+tax3+tax4
  26.         elif earning <= 50000:
  27.             tax1 = 0 #* 1000
  28.             tax2 = 0.1 * 9000
  29.             tax3 = 0.15 * 10200
  30.             tax4 = 0.20 * 10550
  31.             tax5 = 0.25 * (earning - 30750)
  32.             total_tax = tax1+tax2+tax3+tax4+tax5
  33.         else:
  34.             tax1 = 0 #* 1000
  35.             tax2 = 0.1 * 9000
  36.             tax3 = 0.15 * 10200
  37.             tax4 = 0.20 * 10550
  38.             tax5 = 0.25 * 19250
  39.             tax6 = 0.3 * (earning - 50000)
  40.             total_tax = tax1+tax2+tax3+tax4+tax5+tax6
  41.        
  42.         people[key] = total_tax
  43.        
  44.     return people
  45.  
  46. data = {
  47.     'Alex': 500,
  48.     'James': 20500,
  49.     'Kinuthia': 70000
  50. }
  51.  
  52. calculate_tax(data)
  53. print(data)
  54.  
  55. # ---
  56.  
  57. expected_result = {'James': 2490.0, 'Kinuthia': 15352.5, 'Alex': 0}
  58.  
  59. assert data == expected, "Different results"
Add Comment
Please, Sign In to add comment