Victória Greco
Victória Greco

Reputation: 19

Python Shopping cart, tax rate, sum of items

I am creating a shopping cart application applying tax rate and showing the sum of items and total, I could get all the infos showing in the screen except the sum of all items with the taxRate applied. Clearly I'm missing something but I am not sure what I could do to populate price and tax rate for all products and sum them together.

cart = {"Shirt": ["Clothing", 39.99, "Manhattan"], "TV": ["Electronic", 999.00, "White Plains"], "Muffin": ["Bread", 9.50, "Manhattan"], "Jacket": ["Clothing", 45.95, "White Plains"], "Coat": ["Clothing", 239.55, "Manhattan"]}
for i in cart:
    unit = cart[i][1]
    city = cart[i][2]
    kind = cart[i][0]
    taxPercent = getTaxRate(city, kind, unit)
    print ("*tax: ${:.2f}".format(taxPercent*unit), "\n{}:".format(i), unit,"+ ${:.2f}".format(taxPercent*unit),"= ${:.2f}".format(unit + taxPercent*unit))

   # print ("--------- Please pay the following:------- \n", "Total:${:.2f}".format(items + taxPercent*items)) 

Output should be:

*Tax: 00.0
Shirt:  $39.99
TV: 999.0+99.90 = $1098.90
*Tax: 99.90
Muffin: 9.5+0.95 = $10.45
*Tax: 0.95
Jacket: 45.95+4.14 = $50.09
*Tax: 4.14
Coat: 239.55+21.56 = $261.11
*Tax: 21.56
---------- Please pay the following ----------
Total:                    $1460.54

Upvotes: 0

Views: 334

Answers (1)

Bhavya Parikh
Bhavya Parikh

Reputation: 3400

You have to add values to sum it up you can do it by adding statement in code

totalamt=totalamt+unit + taxPercent*unit

it will add the price by iterating loop

cart = {"Shirt": ["Clothing", 39.99, "Manhattan"], "TV": ["Electronic", 999.00, "White Plains"], "Muffin": ["Bread", 9.50, "Manhattan"], "Jacket": ["Clothing", 45.95, "White Plains"], "Coat": ["Clothing", 239.55, "Manhattan"]}


def getTaxRate (city, kind, price):   # given call and simple return
    if city == "Manhattan":
        if kind == "Clothing":
            if price > 100:
                taxRate = 0.09
            else:
                taxRate = 0.0
        elif kind == "Electronic":
            taxRate = 0.11
        else:
            taxRate = 0.1
    elif city == "White Plains":
        if kind == "Electronic":
            taxRate = 0.1
        else:
            taxRate = 0.09
    return taxRate

totalamt=0
for k,v in cart.items():
#     print(v)
    unit = v[1]
    city = v[2]
    kind = v[0]
    taxPercent = getTaxRate(city, kind, unit)
    print("*tax: ${:.2f}".format(taxPercent*unit), "\n{}:".format(k), unit,"+ ${:.2f}".format(taxPercent*unit),"= ${:.2f}".format(unit + taxPercent*unit))
    totalamt=totalamt+unit + taxPercent*unit
print("--------- Please pay the following:------- \n", "Total:${:.2f}".format(totalamt)) 

Upvotes: 1

Related Questions