In: Computer Science
Python - Rewriting a Program
Rewrite Program 1 using functions. The required functions are in the table below.
Create a Python program that will calculate the user’s net pay based on the tax bracket he/she is in. Your program will prompt the user for their first name, last name, their monthly gross pay, and the number of dependents.
The number of dependents will determine which tax bracket the user ends up in. The tax bracket is as follows:
After calculating the net pay, print the name of the user, the monthly gross pay, the number of dependents, the gross pay, the tax rate, and the net pay.
The formula to compute the net pay is: monthly gross pay – (monthly pay * tax rate)
function |
Description |
read_name() |
Reads the name entered, and returns the name. |
read_gross_pay() |
Reads the gross pay amount, and returns the amount. |
read_dependents() |
Reads the number of dependents, and returns the number. |
compute_tax_rate(dependents) |
Computes and returns the tax rate, based on the number of dependents. |
compute_net_pay(gross_pay, rate) |
Computes and returns the net pay, based on the gross pay and the tax rate. |
main() |
Main function of the program. |
Sample run:
Enter your name: Ron Swanson
Enter your gross pay: $3500
Enter number of dependents: 0
Name: Ron Swanson
Gross pay: $3500.00
Dependents: 0
Tax rate: 20%
Net Pay: $2800.00
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new python program with name "main.py" is created, which contains following code.
main.py :
#function to read name from the user
def read_name():
#asking user to enter your name
name=input("Enter your name: ")
return name;#return name
#function to read gross pay amount
def read_gross_pay():
#asking user to enter gross pay amount
grossPay=float(input("Enter your gross pay:$"))
return grossPay #return grossPay
#function to read number of dependents
def read_dependents():
#asking to enter number of dependents
dependents=int(input("Enter number of dependents: "))
return dependents #return
#function to Computes and returns the tax rate
def compute_tax_rate(dependents):
taxRate=0 #variable to store tax rate
#checking number of dependents
if(dependents>=0 and dependents<=1):
#if dependents is from 0 to 1 tax=20%
taxRate=0.20 #set taxRate
elif(dependents>=2 and dependents<=3):
#if dependents is from 2 to 3 tax=15%
taxRate=0.15 #set taxRate
elif(dependents>=4):
#if dependents is greater than or equal to 4, tax=10%
taxRate=0.10 #set taxRate
return taxRate #return tax rate
#function to Computes and returns the net pay
def compute_net_pay(gross_pay, rate):
#calculate and return net pay
return gross_pay-(gross_pay * rate)
#function main
def main():
name=read_name() #call function to read name
grossPay=read_gross_pay() #call function to read_gross_pay
dependents=read_dependents() #call function to read
dependents
taxRate=compute_tax_rate(dependents) #call function to compute tax
rate
netPay=compute_net_pay(grossPay,taxRate) #call function to compute
net pay
print("Name :"+name) #print name
print("Gross Pay :$"+str(grossPay)) #print gross pay
print("Dependents:"+str(dependents)) #print dependents
print("Tax Rate :",(taxRate*100),"%") #print tax rate
print("Net Pay:$"+str((netPay))) #print net pay
#call to main
if __name__=="__main__":
main()
Screen for Indentation :
======================================================
Output : Compile and Run main.py to get the screen as shown below
Screen 1 :main.py
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.