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

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.

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.

emplyee salary calculation using python Error [duplicate]

This question already has answers here:
I'm getting an IndentationError. How do I fix it?
(6 answers)
Closed 2 years ago.
i am beginer of the python programming. i am creating simple employee salary calculation using python.
tax = salary * 10 / 100 this line said wrong error displayed Unindent does not match outer indentation level
this is the full code
salary = 60000
if(salary > 50000):
tax = float(salary * 10 / 100)
elif(salary > 35000):
tax = float(salary * 5 / 100)
else:
tax = 0
netsal = salary - tax
print(tax)
print(netsal)
The error message is self explanatory.
You can't indent your elif and else, they should be at the same level as the if condition.
salary = 60000
if(salary > 50000):
tax = salary * 10 / 100
elif(salary > 35000):
tax = salary * 5 / 100
else :
tax = 0
netsal = salary - tax
print(tax)
print(netsal)
You just need to fix your indentation, I would suggest using an IDE
salary = 60000
if(salary > 50000):
tax = salary * 10 / 100
elif(salary > 35000):
tax = salary * 5 / 100
else:
tax = 0
print(tax)
>>> 6000.0

Calculating returns with trading costs

This perhaps is an over simplification of calculating trading returns while including trading costs. I've made some assumptions - the commission for investing and extracting an investment is 1% and 2% respectively. The commissions do not change over the trading period which in this case is 5 time steps. I've used Python code to perform the calculations.
Set of positive and negative percentage changes in price for a given asset over 5 time steps is {0.031% , 0.00121% , 0.0231% , -0.0213% , -0.0121%}.
The commission to enter an investment is 1% of the invested amount, the commission to exit an investment is 2% of the current value of the invested amount.
If I invest 1 euro in this asset, is the following correct?
1.
The final investment amount if I do not trade the investment until $t=5$ is:
the final percentage change amount at $t=5$ which is 'initial invested amount' + '% change' - 'commission to enter' - 'commission to exit', therefore:
initial_investment_amt = 1
comission_in_amt = 1
comission_out_amt = 2
price_change = -.0121
return_amt = (initial_investment_amt + (price_change / 100)) - (comission_in_amt / 100) - (comission_out_amt / 100) = 0.97 which represents a loss of 1 - .97 = .03
2.
The final investment amount if I trade the investment at each time step until $t=5$ is:
initial_investment_amt = 1
comission_in_amt = 1
comission_out_amt = 2
price_change = .031
return_amt_1 = (initial_investment_amt + (price_change / 100)) - (comission_in_amt / 100) - (comission_out_amt / 100)
price_change = .00121
return_amt_2 = (return_amt_1 + (price_change / 100)) - (comission_in_amt / 100) - (comission_out_amt / 100)
price_change = .0231
return_amt_3 = (return_amt_2 + (price_change / 100)) - (comission_in_amt / 100) - (comission_out_amt / 100)
price_change = -.0213
return_amt_4 = (return_amt_3 + (price_change / 100)) - (comission_in_amt / 100) - (comission_out_amt / 100)
price_change = -.0121
return_amt_5 = (return_amt_4 + (price_change / 100)) - (comission_in_amt / 100) - (comission_out_amt / 100)
print(return_amt_1)
print(return_amt_2)
print(return_amt_3)
print(return_amt_4)
print(return_amt_5)
prints :
0.97031
0.9403220999999999
0.9105530999999999
0.8803400999999998
0.8502190999999998
which represents a loss of $1 - 0.85 = 0.15$.
First, I have to respectfully disagree with your conclusion for case 1:
The final investment amount if I do not trade the investment until $t=5$ is: the final percentage change amount at $t=5$ which is 'initial invested amount' + '% change' - 'commission to enter' - 'commission to exit
The correct formula for the final value, I believe, is
((initial investment amount - commission to enter) * (1 + % change)) - commission-to-exit.
The main difference being the fact that the commission to enter/investment fee is taken out of circulation before the return on investment can be earned. This makes for a significant difference over time.
Assuming I'm correct, below is the code I propose. I took the liberty of changing some of the terminology for ease of reference, but you can obviously change it to whatever suits you:
p_changes = [0.03100, 0.00121, 0.02310, 0.02130, -0.01210]
initial_investment_amt = 100 #I used a larger initial investment; otherwise, the fees would have eaten you alive...
invest_fee = 1
sell_fee = 2
def periodic_ret(amount,change,reinv):
if reinv == 0:
if ind + 1 == 1: #for the initial period
forward = (amount-invest_fee)*(1+change)
if ind +1 == len(p_changes): #for the final period
forward = (amount*(1+change))-sell_fee
else:
forward = (amount*(1+change))
else:
forward = (amount-invest_fee)*(1+change)-sell_fee
print(forward)
return forward
for i in range(len(p_changes)):
reinv = 1 #1 if an invest and sell fee are paid each period, or 0, if an invest fee is paid once upfront and sell fee is paid once at the end
if i == 0: #for the initial period
cur = periodic_ret(initial_investment_amt, p_changes[0], reinv)
else:
cur = periodic_ret(cur,p_changes[i], reinv)
Output (w/ reinv = 1):
100.06899999999999
97.18887348999998
96.41083646761896
95.44308728437926
91.30032592823827
Both the commissions and price changes are given as percentages. This means that immediately after investment the amount in the account is:
initial_investment_amt*(1-commission_in_amt/100)
The _amt suffix is perhaps confusing but commission is stated as % in the question.
After the first investment period the account has the amount:
initial_investment_amt*(1-commission_in_amt/100)*(1-price_change/100)
And finally after exit the client receives:
initial_investment_amt*(1-commission_in_amt/100)(1-price_change/100)(1-commission_out_amt/100)
I think the pattern is clear so you just insert more price_changes for a lengthier investment and if you disinvest and reinvest you'll have a lot more commission to pay.
Hope this is OK - sorry no code - but it seems clearer like this and uses the question notation.

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))

income tax calculation 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.

Categories

Resources