159k views
2 votes
Python Homework

Instructions:

The tax calculator program of the case study outputs a floating-point number that might show more than two digits of precision.

Use the round function to modify the program to display at most two digits of precision in the output number.

Below is an example of the program input and output:

Enter the gross income: 12345.67

Enter the number of dependents: 1

The income tax is $-130.87

# Edit the code below

"""
Program: taxform.py
Author: Ken Lambert
Compute a person's income tax.
1. Significant constants
tax rate
standard deduction
deduction per dependent
2. The inputs are
gross income
number of dependents
3. Computations:
taxable income = gross income - the standard deduction -
a deduction for each dependent
income tax = is a fixed percentage of the taxable income
4. The outputs are
the income tax
"""

# Initialize the constants
TAX_RATE = 0.20
STANDARD_DEDUCTION = 10000.0
DEPENDENT_DEDUCTION = 3000.0

# Request the inputs
grossIncome = float(input("Enter the gross income: "))
numDependents = int(input("Enter the number of dependents: "))

# Compute the income tax
taxableIncome = grossIncome - STANDARD_DEDUCTION - \
DEPENDENT_DEDUCTION * numDependents
incomeTax = taxableIncome * TAX_RATE

# Display the income tax
print("The income tax is $" + str(incomeTax))

User Alhalama
by
5.0k points

1 Answer

3 votes

Answer:

  1. TAX_RATE = 0.20
  2. STANDARD_DEDUCTION = 10000.0
  3. DEPENDENT_DEDUCTION = 3000.0
  4. # Request the inputs
  5. grossIncome = float(input("Enter the gross income: "))
  6. numDependents = int(input("Enter the number of dependents: "))
  7. # Compute the income tax
  8. taxableIncome = grossIncome - STANDARD_DEDUCTION - \
  9. DEPENDENT_DEDUCTION * numDependents
  10. incomeTax = taxableIncome * TAX_RATE
  11. # Display the income tax
  12. print("The income tax is $" + str(round(incomeTax,2)))

Step-by-step explanation:

We can use round function to enable the program to output number with two digits of precision.

The round function will take two inputs, which is the value intended to be rounded and the number of digits of precision. If we set 2 as second input, the round function will round the incomeTax to two decimal places. The round function has to be enclosed within the str function so that the rounded value will be converted to a string and joined with the another string to display the complete a sentence of income tax info.

User Mianos
by
5.6k points