Solved

Getting Variable not defined error in Python

Posted on 2017-04-04
1
99 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Starting up a Project

696 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