?
Solved

Getting Variable not defined error in Python

Posted on 2017-04-04
1
Medium Priority
?
172 Views
Last Modified: 2017-04-04
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

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()

Open in new window

0
Comment
Question by:prodempsey
1 Comment
 
LVL 6

Accepted Solution

by:
prodempsey earned 0 total points
ID: 42079393
Figured it out.  Had to remove the "def main():" tag at the beginning and removed main() at the end.  Worked fine after that.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Screencast - Getting to Know the Pipeline

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question