SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Simple Functions
Exercise:
Programming Exercises
Question:2 | ISBN:9780132576376 | Edition: 2

Question

Programming Exercise #6 in Chapter 2 was the Sales Tax program. For that exercise you were asked to write a program that calculates and displays the county and state sales tax on a purchase. If you have already written that program, redesign it so the subtasks are in functions. If you have not already written that program, write it using functions.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Sales tax python program using functions:

 

#This program displays the Sales tax on purchased amount
#Definition of the main function
def main():
    #Get the purchase amount
    purchaseAmount=getinput()
    print("The amount of the purchase:",purchaseAmount)
    
    statesalestax=calstatesalestax(purchaseAmount)
    print("The state sales tax:",statesalestax)
    
    countysalestax=calcountysalestax(purchaseAmount)
    print("The county sales tax:",countysalestax)
    
    Totalsalestax=caltotalsalestax(statesalestax,countysalestax)
    print("The total sales tax:",Totalsalestax)
    
    Totalofthesale=totalofsale(purchaseAmount,Totalsalestax)
    print("The total of the sale:",Totalofthesale)


#definition of  getinput function   
def getinput():
    #Get the purchase amount
    purchaseAmount = float(input("Enter the amount of a purchase:"))
    return purchaseAmount
    
#Definition of the  calstatesalestax function
def calstatesalestax(purchaseAmount):
    #Assign the value to State sales tax percentage
    Statesalestaxpercentage = 0.04
    #Calculate the state sales tax
    statesalestax = purchaseAmount * Statesalestaxpercentage
    return statesalestax

#Definition of the  calcountysalestax function    
def calcountysalestax(purchaseAmount):
    #Assign the value to county sales tax percentage
    countysalestaxpercentage =0.02
    #Calculate the county sales tax
    countysalestax = purchaseAmount * countysalestaxpercentage
    return countysalestax

#Definition of the  caltotalsalestax function    
def caltotalsalestax(statesalestax,countysalestax):
    #Calculate the total sales tax
    Totalsalestax =  statesalestax + countysalestax
    return Totalsalestax

#Definition of the  totalofsale function    
def totalofsale(purchaseAmount,totalsalestax):
    #Calculate the total of the sale
    Totalofthesale = purchaseAmount + totalsalestax
    return Totalofthesale

# calling the main function
main()

 Sample Output:

Enter the amount of a purchase:2000
The amount of the purchase: 2000.0
The state sales tax: 80.0
The county sales tax: 40.0
The total sales tax: 120.0
The total of the sale: 2120.0
>>> 

 

0 0

Discussions

Post the discussion to improve the above solution.