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

2
  • At a cursory glance, none of your indentation looks right, which would cause issues. Is this exactly how it looks in your file? Commented May 31, 2012 at 17:39
  • I've updated the post and found another error. Commented May 31, 2012 at 17:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

after eliminating the try...except and moving the defines under the while statement, I received another error: Traceback (most recent call last): File "C:/LearningPython/tax2.py", line 32, in <module> main() File "C:/LearningPython/tax2.py", line 20, in main if subtotal.lower()=='exit': AttributeError: 'float' object has no attribute 'lower' -- Thanks for the all the help btw.
@Steven: Wouldn't expect a float to have a String method. How about instead of using a string, you use an integer (say -1) to break the loop? if subtotal == -1: break
That fixed it. Thanks Makoto and Joran.
0
if subtotal.lower()=='exit':
    break

needs to be indented probably ... unless that was just a typo when entering the question

4 Comments

I'd say, "wait for clarification from the OP" before assuming that this is the case. If it is, there's a bit more to indent than just that line.
yeah actually looks like everything below that also needs to be indented ... but certainly those two lines
Yeah, it was the indent problem but now it gives another 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 with the input, though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.