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.