def marginal_tax(income, rate, lower, upper): '''Calculate the marginal tax on income within a specific bracket. Income is the total amount of income, lower and upper are the boundaries of the tax bracket, rate is the tax rate within that bracket. Signature: (int, float, int, int) --> float ''' if income <= lower: bracket_income = 0 elif upper == None or income < upper: bracket_income = income - lower else: # income > upper bracket_income = upper - lower return bracket_income * rate def effective_tax(income): '''Return effective tax rate given the following marginal rates: 0% up to 10k 10% up to 30k 25% up to 60k 35% up to 100k 50% over 100k Signature: (int) --> float''' taxes = 0 taxes += marginal_tax(income, 0.1, 10000, 30000) taxes += marginal_tax(income, 0.25, 30000, 60000) taxes += marginal_tax(income, 0.35, 60000, 100000) taxes += marginal_tax(income, 0.5, 100000, None) return taxes / income * 100 def fruit_purchase(is_ripe, on_sale): '''Return the number of pieces of fruit to purchase, given whether the fruit is ripe and/or on sale. Ripe and on sale --> buy 10 Ripe, not on sale --> buy 5 Not ripe --> buy 0 Signature: (bool, bool) --> int''' if is_ripe and on_sale: return 10 elif is_ripe: return 5 else: return 0 # nested version if is_ripe: # yes, the fruit is ripe if on_sale: return 10 else: return 5 else: # not ripe return 0