Getting Variable not defined error in Python

prodempsey used Ask the Experts™
I'm using Python 3.6, and I'm getting the following error saying my variable 'quantity' is not defined, but it is defined by the users input.  Not sure why I'm getting this error.

Traceback (most recent call last):
  File "C:/Users/prode/Documents/Python Programs/Module3_Prep", line 30, in <module>
    if quantity >= 40:
NameError: name 'quantity' is not defined

def main():

# Get quantity in pounds
    quantity = int(input('Enter the quantity of coffee in pounds.'))

# Calculate the cost of the coffee using if else structure
if quantity >= 40:
    cost = quantity * 7.5
    if quantity >= 20:
        cost = quantity * 8.75
        if quantity >= 10:
            cost = quantity * 10.0
            if quantity < 10:
                cost = quantity * 12.0
                print('You did not enter a valid quantity.')

# Calculate sales tax
    salesTax = cost * .07

# Calculate shipping fee
if cost > 150.0:
    shippingFee = 0
    shippingFee = quantity * 1.0

# Calculate Total Payable
    ttlPayable = cost + salesTax + shippingFee
print('Cost of coffee = $', format(cost, ',.2f'), sep ='')
print('7% Sales Tax = $', format(salesTax, ',.2f'), sep='')
print('Shipping fee = $', format(shippingFee, ',.2f'), sep='')
print('Total payable = $', format(ttlPayable, ',.2f'), sep='')


Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
BI Director
Top Expert 2016
Figured it out.  Had to remove the "def main():" tag at the beginning and removed main() at the end.  Worked fine after that.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial