#python
Вопрос:
Я создаю приложение для корзины покупок, применяющее налоговую ставку и показывающее сумму товаров и общее количество, я мог бы получить всю информацию, отображаемую на экране, за исключением суммы всех товаров с примененной налоговой ставкой. Очевидно, мне чего-то не хватает, но я не уверен, что я мог бы сделать, чтобы заполнить цену и налоговую ставку для всех продуктов и суммировать их вместе.
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))
Результат должен быть:
*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
Комментарии:
1. Просто используйте текущую итоговую переменную…
Ответ №1:
Вам нужно добавить значения, чтобы подвести итог, вы можете сделать это, добавив инструкцию в код
totalamt=totalamt unit taxPercent*unit
это добавит цену путем повторения цикла
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 gt; 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))