emplyee salary calculation using python Error [duplicate] - python

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

Related

Python - Program is returning None? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
def tax(price):
tax = None
# YOUR CODE GOES HERE
def tax(price):
tax = None
if price > 100000 :
tax = (float(price * 20 / 100))
elif price > 75000 and price <= 100000 :
tax = (float(price * 15 / 100))
elif price > 50000 and price <= 75000 :
tax = (float(price * 10 / 100))
else:
tax = (float(price * 5 / 100))
return tax
price = int(input())
print(tax(price))
You have a function called tax within the outer function called tax. The outer function returns nothing and the inner one is never called.
Remove the outer one to get the desired behaviour:
def calculate_tax(price):
if price > 100000 :
tax = (float(price * 20 / 100))
elif price > 75000 and price <= 100000 :
tax = (float(price * 15 / 100))
elif price > 50000 and price <= 75000 :
tax = (float(price * 10 / 100))
else:
tax = (float(price * 5 / 100))
return tax
price = int(input())
print(calculate_tax(price))

What is wrong with this while loop, it seems not ending as the print statement ager it is not executed

I am an absolute beginner in programming (just started yesterday and my major is not CS). I have been struggling with this while loop as I don't know why this print statement is not executed(sometimes it prints as "1"). Please tell me where is wrong. Thanks, guys.
current_saving = 0
# This program intends to calculate the how many months the down
# portion can be paid
annual_input = int(input("Enter your annual salary:"))
portion_saved = float(
input("Enter the percent of your salary to save, as a decimal:"))
total_cost = int(input("Enter the cost of your dream home:"))
portion_down_payment = total_cost * 0.25
months = 0
while current_saving <= portion_down_payment:
current_saving = annual_input / 12 * portion_saved + current_saving * 0.04 / 12
months = months + 1
print(months)
Modify the code to accumulate savings.
current_saving = 0
# This program intends to calculate the how many months the down
# portion can be paid
annual_input = int(input("Enter your annual salary:"))
portion_saved = float(
input("Enter the percent of your salary to save, as a decimal:"))
total_cost = int(input("Enter the cost of your dream home:"))
portion_down_payment = total_cost * 0.25
months = 0
while current_saving <= portion_down_payment:
# Accumulate savings
current_saving = current_saving + annual_input / 12 * portion_saved + \
current_saving * 0.04 / 12
print(f'{current_saving=}') # Comment out this line to not print the accumulation
months = months + 1
print(months)
Test Reults
Enter your annual salary:5000
Enter the percent of your salary to save, as a decimal:.5
Enter the cost of your dream home:10000
current_saving=208.33333333333334
current_saving=417.36111111111114
current_saving=627.0856481481482
current_saving=837.5092669753087
current_saving=1048.6342978652262
current_saving=1260.4630788581103
current_saving=1472.9979557876372
current_saving=1686.2412823069292
current_saving=1900.195419914619
current_saving=2114.862737981001
current_saving=2330.245613774271
current_saving=2546.3464324868523
12

Python while loop maybe need a break statement?

I need for this code to stop calculating the increase of the salary after year 20. I tried adding another while loop but that caused an infinite loop to occur. Any help would be greatly appreciated!!
RATE = 2.0
INITIAL_SALARY = 37238.00
salary = INITIAL_SALARY
year = 1
print("Murdock County")
print("Teacher Salary Schedule")
print()
print("Year Salary")
print("---- ------")
while year < 31 :
print("%4d %15.2f" % (year, salary))
increase = salary * RATE / 100
salary = salary + increase
year = year + 1
You probably typed 31 instead of 21, so
while year < 21 :
# your code
Or you could probably mean that after 21 the salary is no longer increased, so:
while year < 31 :
print("%4d %15.2f" % (year, salary))
if year < 21:
increase = salary * RATE / 100
salary += increase
year += 1
You can use a for loop alternatively because you know the number of times the loop will be executed, and make some improvements as probably removing the variable INITIAL_SALARY, using a string enclosed in tripe double or single quotation marks to avoid using too many print statements:
RATE = 2.0
salary = 37238.00
print("""Murdock County
Teacher Salary Schedule
Year Salary
---- ------""")
for year in range(1, 31):
print("%4d %15.2f" % (year, salary))
if year < 21:
increase = salary * RATE / 100
salary += increase
year += 1
while year < 31 :
print("%4d %15.2f" % (year, salary))
if year < 21:
increase = salary * RATE / 100
salary = salary + increase
year = year + 1

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.

Compound interest with deposits in Python

I am working on an assignment for a comp sci class. I feel like I am really close but I cant quite get to the answer. Basically the assignment is a compound interest calculator, what I am trying to do that makes it more complicated is adding deposits to the initial investment and allowing for someone to stop paying into it at one point, but collect it at a different point. The example is ", a user may already have
saved $10,000 in their account when they start their retirement calculation. They intend to save
another $1000 per year for the next 10 years at which point they will stop making any additional
deposits into their account. However, they may be 20 years away from retirement. Your program
should be able to account for these varying inputs and calculate the correct future value of their
account at retirement"
Here is my code so far:
def main():
print("Welcome to Letmeretire.com's financial retirement calculator!")
import random
num = random.randrange(10000)
principal = int(input("How much are you starting with for your retirement savings?"))
deposit = int(input("How much money do you plan to deposit each year?"))
interest = int(input("How much interest will your account accrue annually"))
time = int(input("Please enter the number of years that you plan to deposit money for."))
time_till_retirement = int(input("How long until you plan on retiring? (Please enter this amount in years)"))
t = time + 1
APR = interest/100
R = ((1+APR/12)**12)-1
DR = deposit/R
DRT = deposit/(R*(1+R)**time)
PV = principal+(DR-DRT)
future_value = PV*((1+APR/12)**12*time)
if time < time_till_retirement:
time1 = (time_till_retirement-time)
future = future_value*((1+APR/12)**12*time1)
else:
future = future_value
for i in range(1, t):
print("After " + str(i) + " years you will have "+ str(future) + " saved!")
main()
I would like the output to look like this:
Enter annual deposit: 1000
Enter interest rate: 12
Enter number of years until retirement: 10
What's the current balance of your account: 5000
How many years will you make your annual deposit? 5
After 1 year, you have: $ 6720.0
After 2 years, you have: $ 8646.4
After 3 years, you have: $ 10803.97
After 4 years, you have: $ 13220.44
After 5 years, you have: $ 15926.9
After 6 years, you have: $ 17838.13
After 7 years, you have: $ 19978.7
After 8 years, you have: $ 22376.14
After 9 years, you have: $ 25061.28
After 10 years, you have: $ 28068.64
But what Im getting is this:
Welcome to Letmeretire.com's financial retirement calculator!
How much are you starting with for your retirement savings?5000
How much money do you plan to deposit each year?1000
How much interest will your account accrue annually12
Please enter the number of years that you plan to deposit money for.5
How long until you plan on retiring? (Please enter this amount in years)10
After 1 years you will have 271235.9643776919 saved!
After 2 years you will have 271235.9643776919 saved!
After 3 years you will have 271235.9643776919 saved!
After 4 years you will have 271235.9643776919 saved!
After 5 years you will have 271235.9643776919 saved!
I think you need to ensure the formula is correct:
FV(t) = 5000 * (1.12 ** t) + 1000 * (1.12 ** t) + 1000 * (1.12 **
(t-1)) + ... + 1000 * 1.12
= 5000 * (1.12 ** t) + 1000 * (1.12 ** t - 1) * 1.12 / 0.12
Then we can define a function:
def fv(t, initial, annual, interest_rate):
return initial * (1+interest_rate) ** t + \
annual * (1+interest_rate) * ((1+interest_rate) ** t - 1) / interest_rate
Test:
print fv(1, 5000, 1000, 0.12)
print fv(3, 5000, 1000, 0.12)
print fv(5, 5000, 1000, 0.12)
Yields:
6720.0
10803.968
15926.8974592
Till now the main work is done, I think you can handle the rest.
In most cases, I would prefer Ray's analytic solution - plug the values into a formula, get the final answer, instead of iterating year by year.
However, in this case, you want the values for each year, so you may as well iterate after all:
import sys
# Python 2/3 compatibility shim
if sys.hexversion < 0x3000000:
rng = xrange
inp = raw_input
else:
rng = range
inp = input
def getter_fn(datatype):
if datatype == str:
return inp
else:
def fn(prompt=''):
while True:
try:
return datatype(inp(prompt))
except ValueError:
pass
return fn
get_float = getter_fn(float)
get_int = getter_fn(int)
def main():
print("Welcome to Letmeretire.com's financial retirement calculator!")
principal = get_float("Initial investment amount? ")
periods = get_int ("How many years will you make an annual deposit? ")
deposit = get_float("Annual deposit amount? ")
apr = get_float("Annual interest rate (in percent)? ") / 100
retirement = get_int ("Years until retirement? ")
deposits = [deposit] * periods
no_deposits = [0.] * (retirement - periods)
amount = principal
for yr, d in enumerate(deposits + no_deposits, 1):
amount = (amount + d) * (1. + apr)
print('After {:>2d} year{} you have: $ {:>10.2f}'.format(yr, 's,' if yr > 1 else ', ', amount))
if __name__ == '__main__':
main()
which results in
Welcome to the Letmeretire.com financial retirement calculator!
Initial investment amount? 5000
How many years will you make an annual deposit? 5
Annual deposit amount? 1000
Annual interest rate (in percent)? 12
Years until retirement? 10
After 1 year, you have: $ 6720.00
After 2 years, you have: $ 8646.40
After 3 years, you have: $ 10803.97
After 4 years, you have: $ 13220.44
After 5 years, you have: $ 15926.90
After 6 years, you have: $ 17838.13
After 7 years, you have: $ 19978.70
After 8 years, you have: $ 22376.14
After 9 years, you have: $ 25061.28
After 10 years, you have: $ 28068.64
FV(t) = 5000 * (1.12 ** t) + 1000 * (1.12 ** t) + 1000 * (1.12 ** (t-1)) + ... + 1000 * 1.12 = 5000 * (1.12 ** t) + 1000 * (1.12 ** t - 1) * 1.12 / 0.12
I have a similar problem like the one mentioned above but I do not get why the second part of the equation(formula) after the second equal sign?
Also is there not another way of doing this more concise without having to code "FV(t) = 5000 * (1.12 ** t) + 1000 * (1.12 ** t) + 1000 * (1.12 ** (t-1))" this part several times?

Categories

Resources