Home > database >  calling calculating functions in Python
calling calculating functions in Python

Time:11-21

it asks for sales tax, but then prints a long number for total tax

#This program will ask user for sales and calcutate state, county, and total sales tax.

#This module calculates the county tax

def askTotalSales():
    totalSales=float(input("Enter sales for the month: "))
    print()
    return totalSales
def countyTax(totalSales):
    countyTax= .02
    
    return totalSales*countyTax
    
def stateTax(totalSales):
    stateTax= .04
    return totalSales *stateTax


#This module calculates the state tax


#this module will calculate total sales tax
def calcTotalTax(stateTax, countyTax):
    totalTax=stateTax countyTax
    print()
    return totalTax

#printData
def printTotalTax (countyTax, stateTax, totalTax):
    print ('County sales tax is' countyTax)
    print('State sales tax is'  stateTax)
    print('Total sales tax is'  totalTax)

def main():
    totalSales=askTotalSales()
    countySales=countyTax(totalSales)
    stateSales=stateTax(totalSales)
    totalTax=float(input(calcTotalTax))
    
    



main()


an online class with zero instruction is not ideal, i've pored throjugh these pages and some youtube videos to come up with this

i understand the issue may be with my Cacltotal tax function- i'm unsure of how to call it

CodePudding user response:

The problem is with this line:

totalTax = float(input(calcTotalTax))

calcTotalTax is a function, not a number. The program will print the function address (which should seem like a random large number) rather than an output. This should be,

totalTax = float(calcTotalTax(stateSales, countrySales))

CodePudding user response:

Pay careful attention to which arguments each function takes (it's specified when you def the function), and make sure you're passing those arguments when you call the function. Also, be careful not to give variables the same names as your functions; each name can only refer to one thing at a time.

In real life you usually don't want print calls in all of your functions, but since you had them in your original code I added strings to them that let you see which function is being called when:

def ask_total_sales():
    return float(input("Enter sales for the month: "))

def calc_county_tax(total_sales):
    print("Calculating county tax...")
    return total_sales * 0.02
    
def calc_state_tax(total_sales):
    print("Calculating state tax...")
    return total_sales * 0.04

def calc_total_tax(county_tax, state_tax):
    print("Calculating total tax...")
    return county_tax   state_tax

def print_total_tax(county_tax, state_tax, total_tax):
    print ('County sales tax is', county_tax)
    print('State sales tax is', state_tax)
    print('Total sales tax is', total_tax)

def main():
    total_sales = ask_total_sales()
    county_tax = calc_county_tax(total_sales)
    state_tax = calc_state_tax(total_sales)
    total_tax = calc_total_tax(county_tax, state_tax)
    print_total_tax(county_tax, state_tax, total_tax)

if __name__ == '__main__':
    main()

Note that (for example) in the main function above, we call the function calc_total_tax with the arguments county_tax and state_tax, and what it returns gets assigned to the value total_tax, which we then pass to print_total_tax along with county_tax and state_tax (which we got by calling calc_county_tax and calc_state_tax).

Enter sales for the month: 500
Calculating county tax...
Calculating state tax...
Calculating total tax...
County sales tax is 10.0
State sales tax is 20.0
Total sales tax is 30.0

CodePudding user response:

Hi there are quite a few things you could improve in your code here to make it clearer for you and anyone else to understand.

I think your functions are unnecessary and removing them and just making those calculations in your main will help for something this simple.

But your main issue will be with calcTotalTax you don't need input but also calcTotalTax takes state tax and county tax as args but you don't have these available in main. Maybe you want to pass it countySales and stateSales?

  • Related