Assignment 7 solutions

Basic solution

Here is my normal solution, using variables tax_sum and nontax_sum to accumulate the sums of just those items that are taxable or not through a series of if statements.

price1 = float(raw_input("Enter price of item 1: "))
price2 = float(raw_input("Enter price of item 2: "))
price3 = float(raw_input("Enter price of item 3: "))

coupon = raw_input("Enter promotional code: ")

print ""
print "                RECEIPT"
print ""

tax_sum = 0.0
nontax_sum = 0.0

if price1 > 10:
    tax_sum = tax_sum + price1
    print "             *Item 1: $", price1
else:
    nontax_sum = nontax_sum + price1
    print "              Item 1: $", price1

if price2 > 10:
    tax_sum = tax_sum + price2
    print "             *Item 2: $", price2
else:
    nontax_sum = nontax_sum + price2
    print "              Item 2: $", price2

if price3 > 10:
    tax_sum = tax_sum + price3
    print "             *Item 3: $", price3
else:
    nontax_sum = nontax_sum + price3
    print "              Item 3: $", price3

tax = tax_sum * 0.05  # 5% sales tax on items over $10
total = nontax_sum + tax_sum + tax

if coupon == "8over20" and total > 20: # 8% off orders over $20
    discount = total * 0.08
elif coupon == "15off":  # 15% off most expensive item
    most_expensive = max(price1,price2,price3)
    discount = most_expensive * 0.15
else:
    discount = 0.


print ""
print "Non-taxable subtotal: $", nontax_sum
print "    Taxable subtotal: $", tax_sum
print "          Tax amount: $", tax
print "               Total: $", total

if discount > 0:
    print "            Discount: $", discount
    print "             YOU PAY: $", total - discount

Advanced solution

In class, I also demonstrated a much more sophisticated solution that could accept any number of items. You indicate that you are finished checking out by entering a zero.

items = []
num = 1
price = float(raw_input("Enter price of item %d: " % num))
while price != 0:
    items.append(price)
    num = num + 1
    price = float(raw_input("Enter price of item %d: " % num))

promo = raw_input("Enter promotional code: ")

print
print "      RECEIPT"
print

for i in items:
    if i > 10:
        print "  *Item: $", i
    else:
        print "   Item: $", i

taxable = sum(i for i in items if i > 10)
nontax = sum(i for i in items if i <= 10)
print " Taxable subtotal:", taxable
print "Non-taxable subtotal:", nontax

tax = taxable * 0.05
print "  Tax:", tax

total = taxable + nontax + tax
print " Total:", total

discount = 0

if promo == "8over20" and total > 20:
    discount = total * 0.08    
if promo == "15off":
    discount = max(items) * 0.15
if discount > 0:
    print " Discount: ", discount
    print " YOU PAY: ", total - discount

Here is an example of running the program:

Enter price of item 1: 17
Enter price of item 1: 5
Enter price of item 1: 21
Enter price of item 1: 6
Enter price of item 1: 4.50
Enter price of item 1: 0
Enter promotional code: 15off

      RECEIPT

  *Item: $ 17.0
   Item: $ 5.0
  *Item: $ 21.0
   Item: $ 6.0
   Item: $ 4.5
 Taxable subtotal: 38.0
Non-taxable subtotal: 15.5
  Tax: 1.9
 Total: 55.4
 Discount:  3.15
 YOU PAY:  52.25