End-To-End-Example: Fudgebonia Taxes

The country of “Fudgebonia” determines your tax rate from the number of dependents:

  • 0 is 30%
  • 1 is 25%
  • 2 is 18%
  • 3 or more is 10%

Write a program to prompt for number of dependents (0-3) and annual income.

It should then calculate your tax rate and tax bill. Format numbers properly! Handle bad input!


In [1]:
## TODO list

# 1. input dependents
# 2. input annual income
# 3. if dependents equals 0 then 
# 3.1   rate is 30%
# 4. else if dependents equals 1 then
# 4.1   rate is 25%
# 5. else if dependents equlas 2 then 
# 5.1   rate is 18%
# 6. else
# 6.1   rate is 10%
# 7. print tax rate
# 8. bill = rate times income
# 8. print tax bill

In [2]:
print("Fudgebonia tax calculator")
dependents = int(input("Enter number of dependents:"))
income = float(input("Enter annual income: "))
if dependents == 0:
    rate = .3
elif dependents == 1:
    rate = .25
elif dependents == 2:
    rate = .18
else:
    rate = .10
print("Tax rate %.2f %%" % (rate))
bill = rate * income
print("Tax Bill $%.2f" %(bill))


Fudgebonia tax calculator
Enter number of dependents:2
Enter annual income: 50000
Tax rate 0.18 %
Tax Bill $9000.00

In [4]:
## Re-write to handle bad input. this should always be a re-write and never attempt in one try.
print("Fudgebonia tax calculator")
try:
    dependents = int(input("Enter number of dependents:"))
    income = float(input("Enter annual income: "))
    if dependents == 0:
        rate = .3
    elif dependents == 1:
        rate = .25
    elif dependents == 2:
        rate = .18
    else:
        rate = .10
    print("Tax rate %.2f %%" % (rate))
    bill = rate * income
    print("Tax Bill $%.2f" %(bill))
except:
    print("You entered a bad value... quitting.")


Fudgebonia tax calculator
Enter number of dependents:fsdg
You entered a bad value... quitting.

In [ ]:
## Re-write to handle dependents < 0
try:
    dependents = int(input("Enter number of dependents: "))
    income = float(input("Enter your annual income: "))
    if dependents >=0:
        if dependents == 0:
            rate = .3
        elif dependents == 1:
            rate = .25
        elif dependents == 2:
            rate = .18
        else: # >=3...
            rate = .10
        print("Tax rate %.2f %%" % (rate))
        bill = rate * income
        print("Tax Bill $%.2f" %(bill))
    else: # dependents < 0 
        print("Invalid number of dependents.")
except ValueError:
    print("Invalid input. Program quitting.")