Assignment 4 solutions

Solution from class

# A4 solution

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: "))
promo = raw_input("Enter promotional code: ")

tax_sum = 0
nontax_sum = 0

print "               RECEIPT"
print

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

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

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

tax = tax_sum * 0.05

print " Non-taxable subtotal: $", nontax_sum
print " Taxable subtotal:     $", tax_sum
print " Tax:                  $", tax
total = price1 + price2 + price3 + tax
print " Total:                $", total

discount = 0
if promo == "10over25":
    if total > 25:
        discount = total * 0.10
    else:
        print "Sorry, that discount code requires total over 25."

if promo == "20off":
##    if price1 >= price2 and price1 >= price3:
##        largest = price1
##    elif price2 >= price1 and price2 >= price3:
##        largest = price2
##    else:
##        largest = price3
    largest = max(price1, price2, price3)
    discount = largest * 0.20

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

Advanced solution, with loops

# Advanced A4 solution, using loops and array

prices = []
price = float(raw_input("Enter price of item: "))
while price > 0:
    prices.append(price)
    price = float(raw_input("Enter price of item: "))

promo = raw_input("Enter promotional code: ")

tax_sum = 0
nontax_sum = 0
print "               RECEIPT"
print

for p in prices:
    if p > 10:
        print "        *Item: $", p
        tax_sum = tax_sum + p
    else:
        print "         Item: $", p
        nontax_sum = nontax_sum + p
        
tax = tax_sum * 0.05

print " Non-taxable subtotal: $", nontax_sum
print " Taxable subtotal:     $", tax_sum
print " Tax:                  $", tax
total = sum(prices) + tax
print " Total:                $", total

discount = 0
if promo == "10over25":
    if total > 25:
        discount = total * 0.10
    else:
        print "Sorry, that discount code requires total over 25."

if promo == "20off":
    largest = max(prices)
    discount = largest * 0.20
    
if discount > 0:
    print " Discount:  $", discount
    print " YOU PAY:   $", total - discount