Python - Program is returning None? [closed] - python

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

Related

calculate years compound interest python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So i have this task, i need to create formula in python, I have all the program running good, but i just can't think of the formula to count years, that's compound interest
what i need to count is how many years will it take to get to the target(lets say from 1500 to 2000)
Got this formula t = ln(A/P) / n[ln(1 + r/n)], but i don't get the right answer
https://www.thecalculatorsite.com/articles/finance/compound-interest-formula.php
Also tried this
https://www.algebra.com/algebra/homework/logarithm/logarithm.faq.question.117944.html
update.
Thanks for help! answer in comments for those who have same issue
This might work:
from math import log
p_start = 1500 # Beginning principal
r = 4.3 # Interest rate
p_end = 2000 # Ending principal
"""
# Lambda function
# n == number of compounding periods per year.
# Default n is 12, or monthly compounding
# Formula Breakdown
round((
log(A / P) /
(n * (
log(1 + (r/n))
)
)
# Add 0.5 to account for needing to round up.
) + 0.5
# Round to zero decimal places
, 0)
"""
get_time = lambda A, P, r, n=12: round((log(A / P) / (n * (log(1 + (r/n))))) + 0.5, 0)
# If interest rate isn't a percentage, convert it
if r > 0:
r /= 100
get_time(p_end, p_start, r, 1) # 7.0 using n = 1, or compounded once per year.
EDIT: Adding solution for comment below:
def calculate_years_to_target(principal, rate, target, n=12):
if rate > 0:
rate /= 100
years = round((math.log(target / principal) / (n * (math.log(1 + (rate/n))))) + 0.5, 0)
return years
calculate_years_to_target(1500, 4.3, 2000, 1)
Compound Interest Calculator:
import math
P = float(input("Enter the initial deposit: "))
A = float(input("Enter the final amount: "))
N = float(input("Enter the number of times the interest is applied per time period: "))
T = float(input("Enter the number of time periods elapsed: "))
interestRate = N*(pow(A/P, 1/(N*T))-1)*100
print("The interest rate is " + str(interestRate) + "%.")

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

Can I use python to calculate tax according to the input income using dictionary? [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 2 years ago.
Improve this question
These are the tax rates:
Net chargeable Income($) Rate Tax($)
On the First 50,000 2%
On the Next 50,000 6%
On the Next 50,000 10%
On the Next 50,000 14%
Remainder 17%
These are the clients and their income:
Client Total Income
Amy 45,000
Bob 80,000
Jane 135,000
Steve 187,500
Hovy 250,000
Will 313,000
I want to write codes to print each client's 2019 taxes as follows:
{'Amy': 900.0, 'Bob': 2800.0, 'Jane': 7500.0, 'Steve': 14250.0, 'Hovy': 24500.0, 'Will': 35210.0}
And my code is like that:
income = {'Amy':45000, 'Bob':80000, 'Jane':135000, 'Steve':187500, 'Hovy':250000, 'Will':313000}
tax = {'Amy':0, 'Bob':0, 'Jane':0, 'Steve':0, 'Hovy':0, 'Will':0}
def question_11(income=income):
client_list=list(income.values())
for i in range(len(client_list)):
if client_list[i]<50000:
tax[i]=0
elif 100000>=client_list[i]>=50000:
tax[i]=1000+(client_list[i]-50000)*0.06
elif 150000>=client_list[i]>100000:
tax[i]=4000+(client_list[i]-100000)*0.1
elif 200000>=client_list[i]>150000:
tax[i]=9000+(client_list[i]-150000)*0.14
elif client_list[i]>200000:
tax[i]=16000+(client_list[i]-200000)*0.17
for name in tax.keys():
(tax.keys[i])=str(name)
for tax_num in tax.values():
(tax[i])=int(tax_num)
dict1={name:tax_num}
print(dic1)
The code has some problem in executing error but I am sure the inner logic of calculating the tax amount is correct. Would you please give me some instructions on that? Thanks!
IIUC: Try this:
def question_11(income_dict=income):
tax_dict = dict()
for person, income in income_dict.items():
if income <= 50000:
tax = 0.02 * income
elif income >= 50000 and income < 100000:
tax = 0.02 * 50000 + 0.06 * (income - 50000)
elif income >= 100000 and income < 150000:
tax = 0.02 * 50000 + 0.06 * 50000 + 0.1 * (income - 100000)
elif income >= 150000 and income < 200000:
tax = 0.02 * 50000 + 0.06 * 50000 + 0.1 * 50000 + 0.14 * (income - 150000)
else:
tax = 0.02 * 50000 + 0.06 * 50000 + 0.1 * 50000 + 0.14 * 50000 + 0.17 * (income - 200000)
tax_dict[person] = tax
return tax_dict
print(question_11())
OUTPUT:
{'Amy': 900.0, 'Bob': 2800.0, 'Jane': 7500.0, 'Steve': 14250.0, 'Hovy': 24500.0, 'Will': 35210.0}

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.

Should I do anything to make my code more pythonic? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
This is a very simple piece of code that I wrote but if there is a way to make it more pythonic then I would love to know. Thanks!
def money():
current_salary = float(input("What is your current salary? "))
years = int(input("How many years would you like to look ahead? ")) + 1
amount_of_raise = float(input("What is the average percentage raise you think you will get? "))
amount_of_raise = amount_of_raise * 0.01
while years > 1:
years = years - 1
new_salary = current_salary + (current_salary * amount_of_raise)
current_salary = new_salary
print('Looks like you will be making', new_salary,' in ', years,'years.')
money()
Extended assignment operators
amount_of_raise = amount_of_raise * 0.01
years = years - 1
x = x * y can be shortened to x *= y. Same thing for -.
amount_of_raise *= 0.01
years -= 1
Iteration and counting
while years > 1:
years = years - 1
Counting down causes your printouts to display backwards. I would count up. The Pythonic way to count uses range:
for year in range(1, years + 1):
print('Looks like you will be making', new_salary,' in ', years,'years.')
Computing new salary
new_salary = current_salary + (current_salary * amount_of_raise)
current_salary = new_salary
I'd probably just simplify that to:
current_salary += current_salary * amount_of_raise
Or even better is to give a 5% raise by multiplying by 1.05. In code that is:
current_salary *= 1 + amount_of_raise

Categories

Resources