Python Forum
How to calculate tax with a variable input and if else
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to calculate tax with a variable input and if else
#1
The following is a lab taken from the course on python, which is what I am having difficulty with:

Estimated time 10-15 minutes Level of difficulty Easy/Medium Objectives Familiarize the student with: • using the if-else instruction to branch the control path; • building a complete program that solves simple real-life problems.

Scenario Once upon a time there was a land - a land of milk and honey, inhabited by happy and prosperous people. The people paid taxes, of course - their happiness had limits. The most important tax, called the Personal Income Tax (PIT for short) had to be paid once a year, and was evaluated using the following rule:

•if the citizen's income was not higher than 85,528 thalers, the tax was equal to 18% of the income minus 556 thalers and 2 cents (this was the so-called tax relief); •if the income was higher than this amount, the tax was equal to 14,839 thalers and 2 cents, plus 32% of the surplus over 85,528 thalers. Your task is to write a simple "tax calculator" - it should accept one floating-point value: the income. Next, it should print the calculated tax, rounded to full thalers. There's a function named round which will do the rounding for you - you'll find it in the skeleton code below. Note: this happy country never returns money to its citizens. If the calculated tax is less than zero, it only means no tax at all (the tax is equal to zero). Take this into consideration during your calculations. Look at the code below - it only reads one input value and outputs a result, so you need to complete it with some smart calculations. Test your code using the data we've provided.

My proposed solution is:

#If tax is less than or equal to 85,528 tax is 18% of income - 556.02.          
#If tax is more than 85,528, tax is 14,839.02 plus 32% of surplus above     85,528.

income = float(input("Enter the annual income: "))
if income <= 85528:
        tax = (income-556.02)*0.18
        # Brackets are to make it do those sums first
    else:
        tax = (income-85528)*0.32 + 14839.02
        # Brackets are to make it do those sums first
tax = round(tax,0)
print("The tax is:", tax)
but when i execute it i have not the right result as output
can you help me to solve it please
likes this post
Reply
#2
How do you know it's not right? Do you have some test cases that are failing? What are the cases and how does your solution differ?
ndc85430 likes this post
Reply
#3
(Aug-14-2020, 06:31 AM)bowlofred Wrote: How do you know it's not right? Do you have some test cases that are failing? What are the cases and how does your solution differ?



in fact this LAB is integrated in a training that I receive, since there are examples of (input / output) when I run the given examples (input) I have not the correct result like (output)

Quote:here are the examples and the results I should have
Sample input: 10000

Expected output: The tax is: 1244.0 thalers

Sample input: 100000

Expected output: The tax is: 19470.0 thalers

Sample input: 1000

Expected output: The tax is: 0.0 thalers

Sample input: -100

Expected output: The tax is: 0.0 thalers
Reply
#4
I got this business percentage programs that you might find it useful and you can also imprement this code to your assignment as well.
This program got 4 choices for user to pick one. Choice #3 is a good example if you need to accumulate prices with percent discount and tax.

URL link:
https://trinket.io/python/656c020f51?tog...Option=run

Gamo
Reply
#5
I suspect that while python has explicit precedence rules and parenthesis to change them, English does not. In particular:

Quote: the tax was equal to 18% of the income minus 556 thalers and 2 cents

can be parsed in two ways. I think the way you have interpreted it is different than what they intended.

Also, I don't see anything in your code that enforces this part of the tax code:

Quote:If the calculated tax is less than zero, it only means no tax at all (the tax is equal to zero).
Reply
#6
(Aug-14-2020, 06:51 AM)Gamo Wrote: I got this business percentage programs that you might find it useful and you can also imprement this code to your assignment as well.
This program got 4 choices for user to pick one. Choice #3 is a good example if you need to accumulate prices with percent discount and tax.

URL link:
https://trinket.io/python/656c020f51?tog...Option=run

Gamo
THANK YOU it is very usefull and welldone
Reply
#7
income = float(input("Enter the annual income: "))
if (10000<= income <= 85528):
    Tax = ((income*18)/100)-556.2
elif income >= 85528: 
    Tax =((income- 85528)*32)/100+14839.2
elif (-100 <= income<=1000): #based on the test data i just entered these no.
    Tax = 0.0
Tax = round(Tax, 0)
print("The tax is:", Tax, "thalers")
buran write Feb-09-2021, 11:25 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#8
Why these huge changes and why all of a sudden only ten cents on a thaler?
You actually need smaller changes of your original program than the ones you made.
Also, there is nothing in the text stating the limits you introduced:
10000<= income <= 85528
and
-100 <= income<=1000
In fact the limit between tax and no tax is an income of 3089 thalers which corresponds to an annual income of exactly 556.02 thalers, but that is not in the assignment text (if you round it down to the nearest thaler, the limit is 3095, if you use the built-in round function, the limit is 3092). You can find it by very simple calculations, though.
I think you original program had the right grasp (with some of the corrections you introduced somewhat simplified):
income = float(input("Enter the annual income: "))
if income <= 85528:
    tax = income * 0.18 - 556.02
else:
    tax = (income - 85528) * 0.32 + 14839.02
tax = round(tax,0) # here you should make it never go negative
print("The tax is:", tax)
It is just the last part left (according to my comment): "If less than zero, make it zero"

Edit: Sorry, didn't notice at first that @shalinisamipillai was the author of the program that I commented on.
Reply
#9
# This how I resolve your code
income = float(input("Enter the annual income: "))

if 3090.0 < income <= 85528.0  : # this income of 3090.0; came from this equation tax=0;which equal to (0.18 * income)=556.2

        tax= ((0.18 * income) - 556.2)

elif income > 85528.0 :

        tax= 0.32*(income - 85528.0)+ 14839.2  

else:
    tax=0
#
# Write your code here.
#

tax = round(tax, 0)
print("The tax is:", tax, "thalers")
Larz60+ write Jul-21-2021, 03:44 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

fixed for you this time. Please use bbcode tags on future posts
Reply
#10
I know this thread is almost a year old and the last post is from several months ago, but I just joined the forum and was checking things out when I noticed this question. Not sure if it will help the OP or not, but hopefully it will help people in the future.

It looked like most of the difficulty was with identifying when the tax should be 0.00 based on the scenario. I'm still relatively new to python so I'm not sure if there is a more efficient way to do this, but this can be solved accurately with nested if statements:

income = float(input("Enter the annual income: "))

if income <= 85528: #income less than or equal to based on scenario
    tax = income * .18 - 556.02 #calculate tax based on input
    if tax <= 0: #if the tax is less than or equal to zero
        tax = 0.00 #tax is 0.00
    else:
        tax = tax #otherwise tax is equal to tax
else:
    tax = (income - 85528) * 0.32 + 14839.02 #income greater than 85528 based on scenario
    if tax <= 0: #same as above
        tax = 0.00
    else:
        tax = tax

tax = round(tax, 0)
print("The tax is:", tax, "thalers")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] How to calculate tax with a variable input and if else rokemas 1 635 Dec-28-2020, 06:53 AM
Last Post: buran
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 1,221 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  How to make a dynamic variable based on input loltylerdowney 3 1,986 Nov-30-2017, 09:49 PM
Last Post: loltylerdowney

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020