# Sales Tax Calculator help please

#1

95% of this code is completed. I just need help getting the calculator to add up the sale total including taxes. I will post what I have below: I think I may have screwed up on my functions somewhere. The specs are:

The sales tax rate should be 6% of the total.
Your program should also contain functions that:
calculate the sales tax
calculate the total after tax
asks the user to enter a number repeatedly until they are done, adds each number to a total variable and uses a counter to determine how many numbers were entered.
outputs the total before tax, the tax and the total including tax.
These functions should round the results to a maximum of two decimal places.
Assume the user will enter valid data.

print(“Sales Tax Calculator”)
print()

# Define variables

choice = "y"
while choice.lower() == “y”:

``````# Define functions

def sales_tax():
print("Sales Tax: ")

def sales_total():
print("Total after tax: ")

# ask prices for each item

print ("Enter the cost of each item (ENTER 0 TO END)")
item_1 = float(input("Cost of item_1:"))
if item_1 == 0:
print (item_1)
raise SystemExit

item_2 = float(input("Cost of item_2:"))
if item_2 == 0:
print("Total:")
total = round(item_1 + item_2, 2)
print(total)
sales_tax()
tax = round(0.06 * total, 2)
print(tax)
sales_total()

choice = input("Again(y/n)? ")
if choice == "y":
continue
if choice == "n":
print("Thanks, bye!")
raise SystemExit

item_3 = float(input("Cost of item_3:"))
if item_3 == 0:
print("Total:")
total = round(item_1 + item_2 + item_3, 2)
print(total)
sales_tax()
tax = round(0.06 * total, 2)
print(tax)
sales_total()
print()
choice = input("Again(y/n)? ")
if choice == "y":
continue
if choice == "n":
print("Thanks, bye!")
raise SystemExit

item_4 = float(input("Cost of item_4:"))
if item_4 == 0:
print("Total:")
total = round(item_1 + item_2 + item_3 + item_4, 2)
print(total)
sales_tax()
tax = round(0.06 * total, 2)
print(tax)
sales_total()
print()
choice = input("Again(y/n)? ")
if choice == "y":
continue
if choice == "n":
print("Thanks, bye!")
raise SystemExit
``````

#2

What if the user wants to add 100 items? You are going to write 100 functions? What about a thousand, you should really use a loop

just declare a variable total, add each item price to it:

``````total = 0
while True:
# input user for price or exit
# check user to exit, then break the loop
# add item price to total

# after the loop, calculate the tax over total price.
# print to inform the user
``````

to break out of the loop, use the `break` keyword, this way you can have code after your loop

if you want to display item 1, item 2 and so on when the user is entering data, just use a variable which you increase each iteration of the loop

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.