Solved

Getting Variable not defined error in Python

Posted on 2017-04-04
1
40 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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 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 …

756 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