?
Solved

Getting Variable not defined error in Python

Posted on 2017-04-04
1
Medium Priority
?
146 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

649 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