income tax calculation python
Asked Answered
U

5

5

How do I go about making a for-loop with a range 70000 and above? I'm doing a for-loop for an income tax and when income is above 70000 there is a tax of 30%. Would i do something like for income in range(income-70000)?

Well, at first i developed a code that didn't use a loop and it worked just fine, but then i was notified that i needed to incorporate a loop in my code. This is what i have, but it just doesn't make sense for me to use a for loop. Can someone help me?

def tax(income):

for income in range(10001):
    tax = 0
    for income in range(10002,30001):
        tax = income*(0.1) + tax
        for income in range(30002,70001):
            tax = income*(0.2) + tax
            for income in range(70002,100000):
                tax = income*(0.3) + tax
print (tax)

Okay, so I have now tried with a while loop, but it doesn't return a value. Tell me what you think. I need to calculate the income tax based on an income. first 10000 dollars there is no tax. next 20000 there is 10%. Next 40000 there is 20%. and above 70000 is 30%.

def taxes(income):

income >= 0
while True:
    if income < 10000:
        tax = 0
    elif income > 10000 and income <= 30000:
        tax = (income-10000)*(0.1)
    elif income > 30000 and income <= 70000:
        tax = (income-30000)*(0.2) + 2000
    elif income > 70000:
        tax = (income - 70000)*(0.3) + 10000
return tax
Ushijima answered 21/11, 2013 at 19:46 Comment(13)
Could you explain what you are trying to do here? Are you building a tax table? What is happening inside the for loop? What happens before / after?Underpinning
Income tax? What do you need a loop for? multiply.Lubricate
Why not just do if income >= 7000? It sounds like you're trying to literally translate the English instruction "for incomes above 7000, levy a 30% tax" into Python, but what for actually does may surprise you.Intercession
Are you sure you want to do that? There are legitimate reasons to do it, but whatever you're doing, it doesn't sound like you should be counting through every income possible.Vocoid
Maybe he's trying to make a Randian point about how high taxes on the top bracket turn the economy into an infinite loop that takes forever to produce nothing. ;)Cyathus
I've added some to my initial question. The problem I have is simple enough. I made a couple if and elif statements and got the correct information, but the problem states that i need to use a loop which seems very unnecessary for the problem at hand.Ushijima
What is your program actually supposed to do?Vocoid
I'm going to guess that when the assignment says, "use a loop", it means, "put the whole program in a while True: loop, so when it finishes calculating the user's income tax, it starts over from the beginning again"Intercession
Kevin, could you elaborate? So what you're saying is to use a while loop followed by a series of if statements?Ushijima
@Ushijima This is clearly an assignment of some type. Could you reproduce the assignment for us? You are not communicating it very clearly.Underpinning
The while wouldn't be in the taxes function, it would go around whatever is calling taxes. See this pastebin for a simple example.Intercession
It's now giving me ValueError: invalid literal for int() with base 10:. It is having a problem with income = int(raw_input('Enter your income: ')).Ushijima
I dont know any python, but your problems is not the language either. You need to read about conditionals. You dont need all that FOR, just 1 and do the IFS according to your rules.Revareval
M
20

Q: How do I go about making a for-loop with a range 70000 and above?

A: Use the itertools.count() method:

import itertools

for amount in itertools.count(70000):
    print(amount * 0.30)

Q: I need to calculate the income tax based on an income. first 10000 dollars there is no tax. next 20000 there is 10%. Next 40000 there is 20%. and above 70000 is 30%.

A: The bisect module is great for doing lookups in ranges:

from bisect import bisect

rates = [0, 10, 20, 30]   # 10%  20%  30%

brackets = [10000,        # first 10,000
            30000,        # next  20,000
            70000]        # next  40,000

base_tax = [0,            # 10,000 * 0%
            2000,         # 20,000 * 10%
            10000]        # 40,000 * 20% + 2,000

def tax(income):
    i = bisect(brackets, income)
    if not i:
        return 0
    rate = rates[i]
    bracket = brackets[i-1]
    income_in_bracket = income - bracket
    tax_in_bracket = income_in_bracket * rate / 100
    total_tax = base_tax[i-1] + tax_in_bracket
    return total_tax
Morvin answered 21/11, 2013 at 19:47 Comment(1)
I asked how to adapt this to work for a list of income values at #58144530Raynell
C
2

Both your function definitely do not compute needed value. You need something like:

import sys

income = 100000
taxes = [(10000, 0), (20000, 0.1), (40000, 0.2), (sys.maxint, 0.3)]

billed_tax = 0
for amount, tax in taxes:
    billed_amount = min(income, amount)
    billed_tax += billed_amount*tax
    income -= billed_amount
    if income <= 0: 
        break

>>> billed_tax
19000.0
Comprehend answered 21/11, 2013 at 20:52 Comment(2)
This answer is incorrect. Consider the second iteration through the loop. amount is 20000 and income is 900000. So billed_amount is 20000. So tax=.1 is applied to 20000. But that tax should only be applied to the amount between 20000 and 10000, which is 10000. And looking at the input, you can see see that a tax of 19000 is too high for this example, because 60000 should be taxed at .2, but the rest should be taxed much less than that, and the result should be less than 19%.Agglomerate
@Agglomerate you're wrong. If you reread the question, 10, 20 and 40 are increments not actual steps. Calculation of increments out of steps is obvious and is OOS of this consideration.Comprehend
K
1

If you really must loop, one approach would be to add up the tax for each unit of income separately:

def calculate_tax(income):
    tax = 0
    brackets = {(10000,30000):0.1, ...}
    for bracket in brackets:
        if income > bracket[0]:
            for _ in range(bracket[0], min(income, bracket[1])):
                tax += brackets[bracket]
    return tax
Kesterson answered 21/11, 2013 at 20:42 Comment(0)
A
1

Let's start with the data (levels and pcts) and some values to test this approach (the test salaries comprises the bracket boundaries)

In [1]: salaries = [5000, 10000, 20000, 30000, 50000, 70000, 90000] 
   ...: levels = [0, 10000, 30000, 70000] 
   ...: pcts = [0, .1, .2, .3]                  

We have a loop on the salaries, we reset the total income tax and next we perform a loop on the salary brackets (expressed in terms of the bottom and the top of each bracket) and the corresponding percentages of taxation.

If salary-bot<=0 we have no income in the next brackets, so we can break out of the inner loop, otherwise we apply the pct for the current bracket to the total amount of the bracket if salary>top otherwise to the part of salary comprised in bracket and add to the total tax.

In [2]: for salary in salaries: 
   ...:     tax = 0  
   ...:     for pct, bottom, top in zip(pcts, levels, levels[1:]+[salary]): 
   ...:         if salary - bottom <= 0 : break  
   ...:         tax += pct*((top-bottom) if salary>top else (salary-bottom)) 
   ...:     print(salary, tax)                                                            
5000 0
10000 0
20000 1000.0
30000 2000.0
50000 6000.0
70000 10000.0
90000 16000.0

It is possible to have no explicit inner loop using generator expressions and the sum builtin.

In [3]: for s in salaries: 
   ...:     bt = zip(levels, levels[1:]+[s]) 
   ...:     s_in_b = (t-b if s>t else s-b for b,t in bt if s-b>0) 
   ...:     tax = sum(p*s for p,s in zip(pcts, s_in_b)) 
   ...:     print(s, tax) 
   ...:                                                                                  
5000 0
10000 0
20000 1000.0
30000 2000.0
50000 6000.0
70000 10000.0
90000 16000.0
Aves answered 1/10, 2019 at 14:30 Comment(0)
R
-1

I dont know any python, but your problems is not the language either. You need to read about conditionals. You dont need all that FOR, just 1 and do the IFS according to your rules.

Revareval answered 21/11, 2013 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.