Thursday, June 13, 2013

Lesson 6 : List & Dictionaries "Grocery Store Project"

It was very good again for today's lesson.


"""
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", "egg", "bread"}



# 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": 2,
    "orange": 32,
    "pear": 15,
    "egg": 10,
    "bread": 15
}
   
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3,
    "bread": 15,
    "egg": 10
}

# Write your code below!
def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] > 0:
          total = total + prices[item]
          stock[item] = stock[item] - 1
    return total

print compute_bill(groceries)


Python 2.7.3 (default, Apr 20 2012, 22:44:07)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>>
set(['orange', 'apple', 'banana'])

5.5
>>>
30.5
>>>
32.5
>>>

No comments:

Post a Comment