Question

I'm quite new to python and this is one of the tasks from codeacademy that I am stuck on. The compute_bill function has to loop through each item in "food (list)" and only add the price of the item to "total (variable)" if the item's stock count is greater than zero. Next, if the item is in stock and after it has added the price to the total, subtract one from the item's stock count.

shopping_list = ["banana", "orange", "apple"]

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 price in food:
        if stock[price] > 0:
            total += prices[price]
    return total

It returns an error saying that *

calling compute_bill with a list containing 1 apples, 3 pears, 1 oranges and 9 bananas resulted in 46.5 instead of the correct 34.5

*. But clearly, it says in the stock dictionary that there are 0 apples, 15 pears, 32 oranges and 6 bananas. Or am I supposed to make a list/dictionary to put in the "food" argument in the compute_bill function?

Any answer would be greatly appreciated.

Was it helpful?

Solution

You are not decrementing stock's count which leads to 3 more bananas(3*4=12) added to total price. You can use dictionary.update() method for decrementation.

def compute_bill(food):
    total = 0
    for item in food:
        if stock[item] > 0:
            total += prices[item]
            stock.update({item: stock[item]-1})

    return total

OTHER TIPS

i think i have a solution, you need to know the number of each item in the list and if it is more that the stock count you need to lower your order to match the number of this item in the stock... never mind , here is a code that you can start with and improve in time :

shopping_list = ["apple","pear","pear","pear","orange","banana","banana",
             "banana","banana","banana","banana","banana","banana","banana"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}

prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
 }



def compute_bill(food):
    banana_number = food.count("banana")
    apple_number = food.count("apple")
    orange_number = food.count("orange")
    pear_number = food.count("pear")
    total = 0
    hash_table = {"apple":apple_number,"banana":banana_number,
                  "orange":orange_number,"pear":pear_number}
    for key in hash_table:
        if hash_table[key]>stock[key]:
            hash_table[key] = stock[key]
        else:
            hash_table[key] = hash_table[key]

        total += prices[key]*hash_table[key]
        stock[key] -= hash_table[key]

    print total
    print stock

if __name__ == '__main__':
    compute_bill(shopping_list)

i hope i have helped @Adrian

You can also decrement by just saying

stock[price]-=1
zozo = ["apple","pear","banana"]

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):
    banana_number = food.count("banana")
    apple_number = food.count("apple")
    orange_number = food.count("orange")
    pear_number = food.count("pear")
    total = 0
    hash_table = {"apple":apple_number,"banana":banana_number,
                  "orange":orange_number,"pear":pear_number}
    for key in hash_table:
        if hash_table[key]>stock[key]:
            hash_table[key] = stock[key]
        else:
            hash_table[key] = hash_table[key]

        total += prices[key]*hash_table[key]
        stock[key] -= hash_table[key]

    return total
    return stock

if __name__ == '__main__':
    compute_bill(shopping_list)
shopping_list = ["banana", "orange", "apple"]

stock = {
    "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}

prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

Code below:

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

    for item in food:
        if stock[item] > 0:
            total += prices[item]
            stock[item]-=1
    return total
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top