-1

I've tried all kinds of things and I have no clue what I am doing wrong. After trying about 5 different ways of doing this I gave in to asking for help on here. What am I doing wrong?

#tried to do this all sorts of ways but gave up
#someone please tell me what the heck I'm doing wrong here

CONSTANT_COUNTY_TAX = 0.02

CONSTANT_STATE_TAX = 0.04

def intro(monthSales):
    print ("This program shows how many taxes you will have to pay on your sales")

def sales():   
    monthSales = eval(input("Enter the total of sales of this month: "))
    return monthSales

def calcCountyTax(sales):
    sales(monthSales)
    countyTaxDue = sales*CONSTANT_COUNTY_TAX
    return countyTaxDue

def calcStateTax(sales):
    sales(monthSales)
    stateTaxDue = sales*CONSTANT_STATE_TAX
    return stateTaxDue

def total(stateTaxDue, countyTaxDue, monthlSales):
    sales(monthSales)
    calcStateTax(monthSales)
    calcCountyTax(monthSales)
    totalSum = sales - (stateTaxDue + countyTaxDue)
    return totalSum


def main():
    intro()
    sales()
    calcStateTax(monthSales)
    calcCountyTax(monthSales)
    total(monthSales, stateTAxDue, countyTaxDue)
chucksmash
  • 5,777
  • 1
  • 32
  • 41
Karl
  • 19
  • 3

3 Answers3

1

There are a couple issues here:

  • You define intro as taking a parameter monthSales but in main you call it with no argument.
  • In sales you are using eval where you don't really need to. Try something like this instead:

    def sales():   
        monthSales = int(input("Enter the total of sales of this month: "))
        return monthSales
    
  • In calcCountyTax you are taking a paramater named sales and then treating it as if it is a function itself. You can use functions as parameters but it probably isn't what you intend here. Perhaps something more like:

    def calcCountyTax(sales):
        countyTaxDue = sales * CONSTANT_COUNTY_TAX
        return countyTaxDue
    
  • Same issue with calcStateTax

  • The monthlSales parameter to total is a typo, should be monthSales.
  • If you are passing precalculated stateTaxDue and countyTaxDue into the total function, you don't need to recalculate the amount of tax due. The total function can be simplified:

    def total(monthSales, stateTaxDue, countyTaxDue):
        totalSum = sales - (stateTaxDue + countyTaxDue)
        return totalSum
    
  • In your main method you aren't storing the values returned from the functions as you call them. main should look more like this:

    def main():
        intro()
        monthlySales = sales()
        monthlyStateTax = calcStateTax(monthlySales)
        monthlyCountyTax = calcCountyTax(monthlySales)
        afterTax = total(monthlySales, monthlyStateTax, monthlyCountyTax)
        print(afterTax)
    
  • You never call the main function. If you just define it but never call it, nothing will happen when you run the script. At the bottom of your script after the definition of main, add:

    def main():
        intro()
        monthlySales = sales()
        ...
        print(afterTax)
    
    main()
    

There are a few other stylistic things (Python names are usually of the form county_tax_due instead of the camel-cased countyTaxDue, for instance), but I've tried to focus specifically on why your script isn't working without making other major changes.

chucksmash
  • 5,777
  • 1
  • 32
  • 41
  • you sure got me a lot closer tot he answer but I am still getting a nasty error Traceback (most recent call last): File "/Users/karlmachleidt/Desktop/Lab 2.7.py", line 39, in main() File "/Users/karlmachleidt/Desktop/Lab 2.7.py", line 36, in main afterTax = total(monthlySales, monthlyStateTax, monthlyCountyTax) File "/Users/karlmachleidt/Desktop/Lab 2.7.py", line 27, in total totalSum = sales - (stateTaxDue + countyTaxDue) TypeError: unsupported operand type(s) for -: 'function' and 'float' – Karl Sep 07 '15 at 14:47
  • I figured it out, but now I am getting -10.2.. if my input is 10 i don't really know how , I changed the total(monthSales...) to total(sales...) – Karl Sep 07 '15 at 15:22
  • I got it , i mixed up the order of input thanks for your help, mate – Karl Sep 07 '15 at 15:36
0

You defined def intro(monthSales): but you are calling it without parameter intro() in main().

Similarly, here you are calling sales function with parameter

def calcStateTax(sales):
sales(monthSales) #

but it is the function doesn't accept any parameters.

def sales():

Also, you are using monthSales variables in different functions but it is not defined as global. This link will help you understand various scopes of python variables.

Community
  • 1
  • 1
sam
  • 2,033
  • 2
  • 10
  • 13
0

Since the others have done you the favor of reviewing your code, here's an approach that's intentionally overkill but is a somewhat more appealing way of going about with a simple calculation problem.

class Tax(object):

    COUNTY = 0.02
    STATE = 0.04

    def __init__(self, sales):
        self.__sales = sales

    @property
    def calc_statedue(self):
        return self.STATE * self.__sales

    @property
    def calc_countydue(self):
        return self.COUNTY * self.__sales

    @property
    def calc_totaldue(self):
        return self.calc_countydue + self.calc_statedue

    @property
    def calc_taxfree_profit(self):
        return self.__sales - self.calc_totaldue


def main():

    print "This program shows how much tax you will need to pay on your sales."
    sales = float(raw_input("Enter the total sales for this month:\n>> "))
    # You can add a check here to see if sales is not non-numerical, perhaps
    # an isdigit, isalpha, float, type, etc. check. Your call.

    tax_calc = Tax(sales)
    print "Taxes due to county: ", tax_calc.calc_countydue
    print "Taxes due to state: ", tax_calc.calc_statedue    
    print "Total taxes due: ", tax_calc.calc_totaldue
    print "Profit after taxes: ", tax_calc.calc_taxfree_profit

if __name__ == "__main__":
    main()

Result:

In [6]: runfile('C:/Users/.../.spyder2/temp.py', wdir='C:/Users/.../.spyder2')
This program shows how much tax you will need to pay on your sales.

Enter the total sales for this month:
>> 1200
Taxes due to county:  24.0
Taxes due to state:  48.0
Total taxes due:  72.0
Profit after taxes:  1128.0

In [7]: 
WGS
  • 13,969
  • 4
  • 48
  • 51
  • I like your answer but sadly this is for a college assignment and I would not be able to explain this, since I am pretty new to Python – Karl Sep 07 '15 at 14:54