-1

I am trying to learn how to create functions. How would I change this code into multiple functions?

purchase = input('Enter the amount of purchase: ')
statetaxes = purchase * 0.05
countytaxes = purchase * 0.025
totaltaxes = (statetaxes + countytaxes)
totalPurchase = (purchase + totaltaxes)

print('The amount of purchase is $'), format(purchase, ',.2f')
print('State tax: $'), format(statetaxes, ',.2f')
print('County tax: $'), format(countytaxes, ',.2f')
print('Total tax: $'), format(totaltaxes, ',.2f')
print('Total: $'), format(totalPurchase, ',.2f')

Would it be something like this:

def main():

    purchase = get_purchase
    statetaxes = get_state
    countytaxes = get_county
    totaltaxes = statetaxes + countytaxes
    totalPurchase = totaltaxes + purchase

    print('The amount of purchase is $', purchase)
    print('State tax: ', statetaxes)
    print('County tax: ', countytaxes)
    print('Total tax: ', totaltaxes)
    print('Total: $'. totalPurchase)

def get_purchase():
    purchase = float(input('Please enter the amount of purchase')
    return purchase

def get_state():
    state = purchase * 0.05
    return statetaxes

def get_county():
    countytaxes = purchase * 0.025
    return countytaxes

main()

Is this correct? If not, where am I going wrong?

I am doing this without a python interpreter because I am using a tablet right now waiting for a flight.

EDIT: What I am trying to do is separate the top program into multiple functions. When I enter this code:

def get_purchase():
    return float(input('Please enter the amount of purchase '))

def get_state():
    return purchase * 0.05


def get_county():
    return purchase * 0.025

def main():

    purchase = get_purchase()
    statetaxes = get_state()
    countytaxes = get_county()
    totaltaxes = statetaxes + countytaxes
    totalPurchase = totaltaxes + purchase

    print('The amount of purchase is $', purchase)
    print('State tax: ', statetaxes)
    print('County tax: ', countytaxes)
    print('Total tax: ', totaltaxes)
    print('Total: $'. totalPurchase)

main()

I get this error:

Please enter the amount of purchase 5000
Traceback (most recent call last):
  File "salestax.py", line 49, in <module>
    main()
  File "salestax.py", line 38, in main
    statetaxes = get_state()
  File "salestax.py", line 27, in get_state
    return purchase * 0.05
NameError: name 'purchase' is not defined

I am getting on the plane now but will check back at the layover to see what I am doing wrong.

Bach
  • 6,145
  • 7
  • 36
  • 61
user3030048
  • 83
  • 1
  • 3
  • 11

3 Answers3

2

You need to call your functions, by adding opening/closing parenthesis:

purchase = get_purchase()

In get_state() you have state and statetaxes variables - you want to use just one of those.

Also, as mhawke mentioned, you need to make get_state and get_county take a purchase parameter, and pass purchase from main to those functions when you call them.

Other than that, your functions appear to properly separate logical operations.

Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
0

Code review:

Move the main() code to the bottom.

def get_purchase(): 
    '''Use docstrings to describe the function'''
    # purchase = float(input('Please enter the amount of purchase')
    # return purchase # no need to assign and then return the assignment
    # just return what you'd otherwise assign:
    return float(input('Please enter the amount of purchase')) # was missing a )

def get_state():
    '''calculate and return state taxes'''
    # state = purchase * 0.05
    # return statetaxes # see a problem here? what is statetaxes?
    return purchase * 0.05 # there, it's fixed!

def get_county():
    # countytaxes = purchase * 0.025
    # return countytaxes
    return purchase * 0.025

So we've moved your main() to the bottom.

def main():

    purchase = get_purchase() # now we're calling these with the ()'s
    statetaxes = get_state()
    countytaxes = get_county()
    totaltaxes = statetaxes + countytaxes
    totalPurchase = totaltaxes + purchase

    print('The amount of purchase is $', purchase)
    print('State tax: ', statetaxes)
    print('County tax: ', countytaxes)
    print('Total tax: ', totaltaxes)
    print('Total: $'. totalPurchase)

Finally, at the bottom of your module, don't just call main(), use the common Python idiom:

if __name__ == '__main__':
    main()

It prevents the code from executing main() if you import the module. When you import a module, its __name__ is no longer '__main__' Relevant: What does if __name__ == "__main__": do?

Community
  • 1
  • 1
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
0

This answer follows your edit where you have altered your code and are now getting a runtime exception.

Functions def get_state() and get_county() both reference a variable named purchase which is not in function's scope.

You should pass purchase into each of the functions like this:

def get_state(purchase):
    return purchase * 0.05

def get_county(purchase):
    return purchase * 0.025

def main():
    purchase = get_purchase()
    statetaxes = get_state(purchase)
    countytaxes = get_county(purchase)

There are other ways to do this, e.g. a class based approach, however as you stated that you are learning functions, this is probably the way to go.

mhawke
  • 84,695
  • 9
  • 117
  • 138