Calculating sales taxes using functions in python
Question: I can't get this program in Python 3.3.5 to run and not sure what I did wrong.
The program is to ask the user to enter the total sales for the month, then
calculate and display the the following, the amount of county sale (county
sales tax is 2.5percent) and amount of state sales tax (state sales taxrate is
.05) and the total sales tax (county plus state) I've copied and pasted what
I've done so far.

    
    
    # Show individually the 5 purchases amounts, compute the state and county sales
    # tax on those combined  purchases and calculate the total of taxes
    # and purchases
    county_sales_tax =  .025 
    state_sales_tax = .05
    
    # Enter the purchase price
    def main():
      purchase = float(input('Enter the price of the purchase: '))
      calculate_totals(purchase)
    
    # Calculate the purchase price with the coounty and state sales tax
    def calculate_totals(purchase):
      county_sales_tax = purchase * .025 
      state_sales_tax = purchase * .05
      total_sales_tax = county_sales_tax + state_sales_tax
      total_price = purchase + state_sales_tax + county_sales_tax
    
    
    # display the amount of the purchases, county and state sales
    # taxes, combined amount of the both taxes and total sale with taxes
    def display_totals(purchase, state_sales_tax, county_sales_tax, total_taxes, total_price):
      print('The purchase price is $, ')
      print('The county_sales_tax is $', \
        format (county_sales_tax, '.2f'))
      print('The state_sales_tax is $', \
        format (state_sales_tax, '.2f'))
      print('The total_sales_tax is $:, ')
      print('The total_price is $:, ')
    
    # Call the main function
    main()
    


Answer: I fixed up a few things, I'll try to walk through where some of the mistakes
were.

    
    
    def calculateStateTax(price):
        state_sales_tax = .05
        return price * state_sales_tax
    
    def calculateCountyTax(price):
        county_sales_tax =  .025
        return price * county_sales_tax
    
    def displayTotals(price):
        print('Original price', price)
        state_tax = calculateStateTax(price)
        print('State tax', state_tax)
        county_tax = calculateCountyTax(price)
        print('County tax', county_tax)
        print('Total', price + state_tax + county_tax)
    
    def main():
        price = float(input('Enter the price of the purchase: '))
        displayTotals(price)
    

  1. Indentation is super important in Python! The indentation is used to define the scope of things, note that all the code that is within a defined function is tabbed (or 4 spaces) over

  2. In general, you can use functions input parameters and return values to pass things around, instead of declaring global variables.

  3. Note that the `displayTotals` function calls the other functions, and gets values out of them via `return`. This means `displayTotals` doesn't need all those parameters to be passed in.

  4. It is generally better to only define constants within the scope that they need to exist (e.g. `state_sales_tax` doesn't need to be global).

