income tax calculation python - python

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

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

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

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

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

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.

Related

Calculate total amount from Upwork after fee deduction using Python

I am trying to calculate total money after deduction of a Fee from Upwork. I found this website that does this but I want to make it using Python. Here is the guidelines from Upwork:
$0-$500 in earnings from a client: 20% service fee applied to earnings.
$500.01-$10,000 in earnings from a client: 10% service fee.
$10,000.01 or more in earnings from a client: 5% service fee.
The code:
budget = int(input("Enter the price (USD): $"))
if 0 <= budget <= 500:
cut = int((20/100)*budget)
budget = budget - cut
elif 501.01 <= budget <= 10000:
cut = int((10/100)*budget)
budget = budget - cut
elif budget >= 10000.01:
cut = int((5/100)*budget)
budget = budget - cut
print(f'Total price after Upwork Fee is ${budget}.')
According to Upwork,
On a $600 project with a new client, your freelancer service fee would be 20% on the first $500 and 10% on the remaining $100. Your earnings after fees would be $490.
I made this calculator but it works only for the first condition $0-$500 in earnings. If the budget is $600, I will get $490 but here I am getting $540. Can someone help me out what went wrong?
--Update--
I also tried this but of no use:
budget = int(input("Enter the price (USD): $"))
a = 0 <= budget <= 500
b = 501.01 <= budget <= 10000
c = budget >= 10000.01
cut1 = int((20/100)*budget)
cut2 = int((10/100)*budget)
cut3 = int((5/100)*budget)
if a:
cut = cut1
budget = budget - cut
if a and b:
cut = cut2
budget = budget - cut
if a and b and c:
cut = cut3
budget = budget - cut
print(f'Total price after Upwork Fee is ${budget}.')
budget = int(input("Enter the price (USD): $"))
def discount(money,percent):
cut = int((percent/100)*money)
money= money- cut
return money
if 0 <= budget <= 500:
budget = discount(budget ,20)
elif 500.01 <= budget <= 10000:
budget2 = discount(500 ,20)
budget = discount(budget-500 ,10)
budget = budget + budget2
elif budget >= 10000.01:
budget3 = discount(500 ,20)
budget2 = discount(10000 ,10)
budget = discount(budget-10500 ,5)
budget = budget + budget2 + budget3
print(f'Total price after Upwork Fee is ${budget}.')
If I understand you correctly, two things are wrong here:
you substract the cut from the budget.
you apply the cut to the max value, not the "diapason" value.
My guess is that you need something like this pesudo-code:
if budget > 0
cut+= 0.20*MIN(Budget, 500)
if budget > 500
cut+= 0.10*MIN(Budget-500, 9500) // We already applied the cut to the first 500
if budget > 10000
cut+= 0.05*(Budget-10000) // we already applied a cut to the first 10000
So:
do not modify budget
accumulate cut
substract it at the end.

Yearly Interest on house and deposit

Suppose you currently have $50,000 deposited into a bank account and the account pays you a constant interest rate of 3.5% per year on your deposit. You are planning to buy a house with the current price of $300,000. The price will increase by 1.5% per year. It still requires a minimum down payment of 20% of the house price.
Write a while loop to calculate how many (integer) years you need to wait until you can afford the down payment to buy the house.
m = 50000 #money you have
i = 0.035 #interest rate
h = 300000 #house price
f = 0.015 #amount house will increase by per year
d= 0.2 #percent of down payment on house
y = 0 #number of years
x = 0 #money for the down payment
mn = h*d #amount of down payment
while m <= mn:
m = (m+(m*i)) #money you have plus money you have times interest
y = y + 1 #year plus one
mn = mn +(h*f*y)
print(int(y))
The answer you should get is 10.
I keep getting the wrong answer, but I am not sure what is incorrect.
You can simplify the code by using the compound interest formula.
def compound_interest(amount, rate, years):
return amount * (rate + 1) ** years
while compound_interest(m, i, y) < d * compound_interest(h, f, y):
y += 1
If you are allowed to do without the while loop, you can resolve the inequality after the years y.
So you get this code snippet:
import math
base = (i + 1) / (f + 1)
arg = (d * h) / m
y = math.ceil(math.log(arg, base))

How can I increase a value by 5% monthly using a while loop?

I'm attempting to take a starting balance and increase the value by 5% each month. I then want to feed this new balance back into the equation for the next month. I've attempted to do this using a while loop but it doesn't seem to be feeding the new balance back in.
I'm using 60 months (5 years) for the equation but this can be altered
counter = 1
balance = 1000
balance_interest = balance * .05
while counter <= 60:
new_monthly_balance = (balance + balance_interest)*(counter/counter)
print(new_monthly_balance)
balance = new_monthly_balance
counter += 1
You never change balance_interest in the loop.
What do you intend to do with *(counter/counter)? This merely multiplies by 1.0, which is a no-op.
while counter <= 60:
balance *= 1.05
print(balance)
counter += 1
Better yet, since you know how many times you want to iterate, use a for:
for month in range(60):
balance *= 1.05
print(balance)
BTW, just what sort of finance has a constant 5% monthly increase???

I can't understand how this (function?) works

I'm pretty new to Python and I'm going through a starter book. The code isn't written in English so I tried my best to translate, hope you guys understand.
It has this exercise where we calculate the taxes from the user salary:
salary = float(input("Enter your salary to taxes calculation: "))
base = salary
taxes = 0
if base > 3000:
taxes = taxes + ((base - 3000) * 0.35)
base = 3000
if base > 1000:
taxes = taxes + ((base - 1000) * 0.20)
My problem is when the input is bigger than 3000, for example, if I run the code with the salary of 5000, the result will be 1100. But when I do the 'same' math on the calculator the result is 700, so I'm lost in here, could someone explain it please?
Please note that in case of salary 5000, the control will go to both the if statements. So it comes out as 700 from first, and 400 from second, therefore answer is 700+400. This also makes sense, as tax calculation is mostly partitioned in brackets, and is not a flat percentage on salary.
Alright, let's walk through it with your example of 5000
salary = float(input("Enter your salary to taxes calculation: "))
base = salary
# base = 5000
taxes = 0
if base > 3000: # base is larger than 3000, so we enter the if statement
taxes = taxes + ((base - 3000) * 0.35)
# taxes = 0 + ((5000 - 3000) * 0.35)
# taxes = 0 + 700
# taxes = 700
base = 3000 # base is set to 3000
if base > 1000: # base was set to 3000 in the line above, so we enter the if statement
taxes = taxes + ((base - 1000) * 0.20)
# taxes = 700 + ((3000 - 1000) * 0.20), remember taxes is already 700 from above
# taxes = 700 + 400
# taxes = 1100
since it is two if statements and not an if and an else we evaluate both statements when base is set larger than 3000. I hope that helps.
It flows on to the second function
so if I sub in the numbers:
Salary = 5000
base = 5000
taxes = 0
if 5000 > 3000:
taxes = 0 + ((5000- 3000) * 0.35) # = 700
base = 3000
if 3000 > 1000:
taxes = 700 + ((3000 - 1000) * 0.20) # = 1100
This is an economical equation which calculate tax for every part of salary.
the procedure would be this:
For amount greater than 3000 calculate 35% tax for this portion of salary.
For amount greater than 1000 (and less than 3000) calculate 20% tax for this portion of salary.
Tax over salary would be the summation of this taxes.

What is an efficient way to calculate cost constrained by thresholds

I want to calculate cost of water expense of households constrained by certain thresholds:
Basically the thresholds are as follows:
for the first 0-10 m3 it will cost $10
for the subsequent 11-20 m3 it will cost $20
for the subsequent 21-30 m3 it will cost $30
for the subsequent >30 m3 it will cost $40
For example, if a household uses 40 m3 of water, it will be charged:
(10*10) + (10*20) + (10*30) + (10*40) = $1000
if a household uses 23 m3 of water, it will be charged:
(10*10) + (10*20) + (3*30) = $390
The only way I can think of is using if-conditionals. And I don't think that's the best way to calculate this.
You could also use a dictionary:
dic = {0:(0,10), 1:(100,20), 2:(300,30), 3:(600,40)}
Then you only need once an if-statement.
def costs(vol):
interval = vol/10 # vol must be positive int
if interval in dic:
price = dic[interval]
return price[0] + price[1]*(vol-price[1] + 10)
else:
return dic[3][0] + dic[3][1]*(vol-dic[3][1] + 10)
You can use the fact that the cost function is piecewise linear. Thus, you have to find out at which interval you are and use this as the key to your dictionary, where you save the cost at the beginning of each interval aswell as the increase of cost.
First define a list which will hold these limits in (difference in volume, price) format:
lst = [(10, 10), (10, 20), (10, 30), (None, 40)]
Note that the order matters. Here None stands for "no limit". And now the function:
def get_money(volume):
if volume <= 0:
return 0
total = 0
for diff, price in lst:
if volume == 0:
break
if diff is None:
total += volume * price
volume = 0
elif volume > diff:
total += diff * price
volume -= diff
else:
total += volume * price
volume = 0
return total
BTW: There is no way to solve this without using if. :)

Categories

Resources