Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8042213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:30:02+00:00 2026-06-05T04:30:02+00:00

UPDATED: This Code Works: # North Carolina Sales Tax Estimator # Estimates the Amount

  • 0

UPDATED:

This Code Works:

# North Carolina Sales Tax Estimator
# Estimates the Amount of Tax You Should Pay in North Carolina

# Defines the Current 2012 Tax Rate
nctaxrate = 0.07

# Defines the tax variable by multipling subtotal by the current nc tax rate
# tax = subtotal * nctaxrate

# Defines the total variable by adding the tax variable to the subtotal variable
# total = subtotal + tax

# Defines the Subtotal from an Input of the User's Purchase Amount
def main():
    print("\t\t\tThis is the NC Sales Tax Estimator")
    print("\t\t Input Your Total Purchases Below\n")

    while True:
        subtotal = float(input("Enter the total price of your purchases:\t$").strip())
        if subtotal == -1: break
        tax = subtotal * nctaxrate
        total = subtotal + tax

        print("\tSUBTOTAL: $", subtotal)
        print("\t     TAX: $", tax)
        print("\t   TOTAL: $", total)

# if this script is called directly by Python, run the main() function
# (if it is loaded as a module by another Python script, don't)
if __name__=="__main__":
    main()

Here is the Original Question:

So I am learning Python and asked a previous question yesterday and was supplied with an awesome set of code that I decided to modify to work with an NC Sales Tax Estimator Program I wanted to create.

The one thing is that I am getting a break out loop error that I don’t quite understand. I’ve searched and tried to understand the meaning but I know the code worked before. Also the tax code program I created from scratch 🙂 worked before trying to add the fancy ability to submit many inputs in a loop until the user wanted to ‘exit’.

Here is the Code:

# North Carolina Sales Tax Estimator
# Estimates the Amount of Tax You Should Pay in North Carolina

# Defines the Current 2012 Tax Rate
nctaxrate = 0.07

# Defines the tax variable by multipling subtotal by the current nc tax rate
tax = subtotal * nctaxrate

# Defines the total variable by adding the tax variable to the subtotal variable
total = subtotal + tax

# Defines the Subtotal from an Input of the User's Purchase Amount
def main():
    print("\t\t\tThis is the NC Sales Tax Estimator")
    print("\t\t Input Your Total Purchases Below")

while True:
    subtotal = float(input("Enter the total price of your purchases (or 'exit' to quit) :\$").strip())
    if subtotal.lower()=='exit':
        break

    try:
        subtotal = int(subtotal)
    except ValueError:
        print("That wasn't a number!")

    try:
        print("\tSUBTOTAL: $", subtotal)
        print("\t     TAX: $", tax)
        print("\t   TOTAL: $", total)
    except KeyError:
        print("")

# if this script is called directly by Python, run the main() function
# (if it is loaded as a module by another Python script, don't)
if __name__=="__main__":
    main()

P.S. I only added the KeyError because I researched that you must have an error statement after a try. I’m just a beginner so I’m attempting to create programs myself and reading “Python for the Absolute Beginner”.

UPDATE:

I fixed the indentation but now I get the following traceback error:

Traceback (most recent call last):
  File "C:/LearningPython/taxestimator.py", line 30, in <module>
    tax = subtotal * nctaxrate
NameError: name 'subtotal' is not defined

I thought I defined it in the input ie.

subtotal = float(input("Enter the total price of your purchases (or 'exit' to quit) :\$").strip())

Is it because the other defines (tax and total) that use the defined subtotal are defined before subtotal is defined? I tried moving them below the defined subtotal but it didn’t work still.

Thanks for any Advice.

Best,

Steven

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:30:03+00:00Added an answer on June 5, 2026 at 4:30 am

    The main issue that you have is that Python requires whitespace to scope methods. Without that, then statements will run not as intended – for example:

    while True:
        subtotal = float(input("Enter the total price of your purchases (or 'exit' to quit) :\$").strip())
    if subtotal.lower()=='exit':
        break
    

    will not break out of the loop with valid input (it will if you put a string there, but that’s another matter). Also, if everything is to be in the scope of main(), every statement requires one level of indentation (or four lines of whitespace, if you prefer). As it stands, your while won’t run in the scope of main().

    Also, you make reference to subtotal before you actually give it a value. Since subtotalwasn’t properly initialized, you won’t have a value to use for it.

    You would want to rewrite your code such that tax and total are defined after you define subtotal.

    while True:
        subtotal = float(input("Enter the total price of your purchases (or 'exit' to quit) :\$").strip())
        if subtotal.lower()=='exit':
            break
        tax = subtotal * nctaxrate
        total = subtotal + tax
    

    Finally, if subtotal, total, and tax are properly defined (which, after the above, they will be), there will be no need for the superflous try...except statement when you wish to print the values out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code have been working until I updated last night. The code works perfectly
Is there anything wrong with this code? My entity is not getting updated. public
The following code works as you’d expect — MyProperty on the model is updated
Unfortunately this mess works: Do you have suggestings for cleaning up this code: I'm
THE CODE BELOW IS UPDATED CODE THAT WORKS I am getting a syntax error
This code works to output a piano tone for 2 seconds using winmm.dll via
I have this code that works in FreePascal under Windows and need to translate
for pos, char := range s { fmt.Println( utf8.RuneLen(char) ) } This code works
So I run this Windows Server 2008 security update and this code block is
I use this code to update data in database table. Can reuse same code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.