Unit 3 Problems Answers

U3_answers

Things to consider:

Yes, there are repeats in the list. It doesn't matter for this program, because iterating through the list will result in it counting the repeats. The items in the list are indexed, and the value at each index is passed through in the iteration. Consider the following:

In [9]:
# a list with repeats
l = ["Hey", "Hey", "Hey"]

# iterate and print
for item in l:
    print(item)
Hey
Hey
Hey

Each of the "Hey"s would get looked up in a dict in our program, so we don't need to worry.

I'll hit each of the other answers to the "things to think about" in the comments.

In [1]:
# here is a your shopping list 
shopping_list = ["MegaDeath Sauce", "Maddog 357", "Secret Aardvark", 
        "Tapitio","High River Rogue","Da'Bomb",
        "Red Hot","Queen Majesty","Pain 100%",
        "Secret Aardvark","Siracha","Cholula",
        "Red Hot","The Last Dab", "Red Hot"]

# here is the dict mapping the item to their prices
price = {"MegaDeath Sauce" :7.99, "Maddog 357":8.99,
         "Secret Aardvark" :5.99, "Tapatio"   :2.95,
         "High River Rouge":8.95, "Da'Bomb"   :7.99,
         "Queen Majesty"   :9.99, "Pain 100%" :10.48, 
         "Cholula"         :3.65, "Red Hot"   :7.49,
         "The Last Dab"    :20.00 }

# we start with a cost stored as 0
cost = 0

# we iterate though the shopping list
for item in shopping_list:
    # we use increment increase the cost by the price of the item
    # note we use get() in case we don't find the item
    # in this case, add 0 using the second argument.
    cost += price.get(item, 0)

# we have now read each item in the list and added the price to cost    
    
# print the price
print("Total Cost:", cost)

# multiply by 1.06 to get the total after tax
cost_w_tax = cost * 1.06

# print the total after tax
print("After Tax:", cost_w_tax)

# greater than or equal to 85?
if cost_w_tax >= 85:
    print("You have spent 85 dollars or more")
Total Cost: 103.53999999999999
After Tax: 109.7524
You have spent 85 dollars or more