prodempsey
asked on
Getting Variable not defined error in Python
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 Program.py", line 30, in <module>
if quantity >= 40:
NameError: name 'quantity' is not defined
Traceback (most recent call last):
File "C:/Users/prode/Documents/
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
else:
if quantity >= 20:
cost = quantity * 8.75
else:
if quantity >= 10:
cost = quantity * 10.0
else:
if quantity < 10:
cost = quantity * 12.0
else:
print('You did not enter a valid quantity.')
# Calculate sales tax
salesTax = cost * .07
# Calculate shipping fee
if cost > 150.0:
shippingFee = 0
else:
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='')
main()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.