Python Forum

Full Version: How to calculate tax with a variable input and if else
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
#RESOLVI DESTE JEITO E FUNCIONA PERFEITAMENTE

income = float(input("Enter the annual income: "))
if 1000 < income <= 85528:
    tax = (income*18/100)-556.02
elif income >= 85528:
    tax =(income- 85528)*32/100+14839.02
elif -100 <= income <= 1000: 
    tax = 0.0
tax = round(tax, 0)
print("The tax is:", tax, "thalers")
(Jul-21-2021, 02:53 PM)treed226 Wrote: [ -> ]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")

Hi, thanks for solving the problem in a very simple and easy way to understand. Your coding is a per the requirement provided. Really thanks a lot!! It helped me to think other problems in a simplest way.
income = float(input("Enter the annual income: "))
if income <= 85528:
    tax = income *0.18 - 556.2
       
else:
    tax = (income - 85528)*0.32 + 14839.2
        
tax = round(tax, 0)
print("The tax is:", tax, "thalers")
gave me the expected outcome.
Pages: 1 2