Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to find the tax for 2 items in a list

So I’m trying to write a calculator in python to get the total price of an infinite amount of items after-tax however I’m not sure how to do this.

    while True:
        after_tax = 0
        before_tax = []
        before_tax.append(int(input("How much is the item: ")))
    
        more = input("Anymore items? (y/n): ")
        if more.lower() == "n":
            after_tax = (before_tax[0] - (before_tax[0] * 0.1) + (before_tax[0] * 0.0845))
            print(after_tax)
            break
        else:
            for i in before_tax:
                after_tax += i

result:

How much is the item: 10
Anymore items? (y/n): y
How much is the item: 10
Anymore items? (y/n): n
9.845

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s not necessary to keep a list of the items; just sum the before-tax costs as they’re entered and then multiply by the appropriate rate at the end.

before_tax = 0
while True:
    before_tax += int(input("How much is the item: "))
    more = input("Anymore items? (y/n): ")
    if more.lower() == "n":
        break

tax_rate = 1.0845  # 8.45% sales tax
print(f"Before tax: {before_tax}")
print(f"After tax: {before_tax * tax_rate}")
How much is the item: 10
Anymore items? (y/n): y
How much is the item: 10
Anymore items? (y/n): n
Before tax: 20
After tax: 21.69

If you did have a list of numbers (maybe if there’s something else you need to get out of the individual prices, like average, minimum/maximum, etc, such that only having the sum wouldn’t do), the way to get the sum of the list is to use the builtin sum function:

before_tax = []
while True:
    before_tax.append(int(input("How much is the item: ")))
    more = input("Anymore items? (y/n): ")
    if more.lower() == "n":
        break

tax_rate = 1.0845  # 8.45% sales tax
print(f"Before tax: {sum(before_tax)}")
print(f"After tax: {sum(before_tax) * tax_rate}")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading