Thursday, June 13, 2013

Thursday Lesson



Today, we learn about groceries listing in our python programming class

click this more lesson
www.dropbox.com/s/r8f1hms97nw5mc2/Lesson6-GroceryShopper.py


"""
Learning Python
Lesson 6 : List & Dictionaries
Grocery Store Project

Simple programming exercises from Codecademy
"""


# Exercise 0 : Shopping at the Market
"""
    First, make a list (not a dictionary!) with the name groceries.
    Insert a "banana", "orange", and "apple".
"""
groceries = ["banana", "orange", "apple", "apple", "banana", "orange", "apple", "apple", "banana", "orange", "apple", "apple" ]


# Exercise 1 : Making a Purchase
"""
    Write a function compute_bill that takes an argument food as input
    and computes your bill by looping through your food list and adding
    the costs of each item in the list.

    If an item on your food list is not in stock, don't worry about it for now.
"""

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
   
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!

def compute_bill (food):
    total = 0
    for item in food:
        if stock[item]>0:
            total = total + prices[item]
    return total

print compute_bill(groceries)
       





# Exercise 2 : Stocking Out
"""
    Do the following for your compute_bill function:

        Do not add the price of an item into your list if it is out of stock.

        After you buy an item, subtract one from its stock.

        If an item is in your list multiple times you must repeat
        this process multiple times.

    Once you think you've got it, uncomment the line of code below to check out!
"""

#print compute_bill(groceries)

No comments:

Post a Comment