Stocking out?


#1



Hola a todos, este ejercicio Stocking Out, me esta sacando más canas de las que ya tengo, según las indicaciones se desarrollo el código como se solicita, una vez terminado al momento de ejecutarlo devuelve el error:

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in 27 instead of the correct 7

Éste error hace referencia a que el código usado no podrá calcular el ejercicio además de presentar una nueva cualidad en el: usar un Dictionary en vez de la List sugerida en el ejercicio, ya que los items tienen ahora cantidades.
Dentro de está también se plantea el caso de que la cantidad del item de la lista sea mayor a la cantidad del item Stock en tal caso pide solo vender la cantidad del item existente en Stock, como en el caso del error que copio arriba.
Ojo, aquí uso dentro de la función el mismo nombre del Dictinonary para comprobar su funcionamiento, lo correcto seria usar el nombre del argumento, pero me da el error:
"Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: list indices must be integers, not str"


shopping_list = {"banana": 9, "apple": 2, "orange": 1, "pear": 1}
#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 item in food:
        if stock[item] > 0 and stock[item] >= shopping_list[item]: 
            #shopping_list[itm] = stock[itm]
            total = total + (shopping_list[item] * prices[item])
            #stock[itm] = stock[itm] - 1
            #stock[itm] = stock[itm] - shopping_list[itm]
        elif stock[item] < shopping_list[item]:
            shopping_list[item] = stock[item]
            total = total + (shopping_list[item] * prices[item])
            #stock[itm] = stock[itm] - 1
            #stock[itm] = stock[itm] - shopping_list[itm]
    #print total
    return total

print compute_bill(shopping_list)

Alguien tiene idea del porqué?


#2

The local object is food so use food[item] to get at the count value.

Do not use shopping_list inside the function. Use food.


#3

Lo aclare, lo use para corroborar que el Dictionary sea leído correctamente, ya que al usar "food" me da el error: "Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: list indices must be integers, not str"


#4

Let's go again.
The code as requested by the exercise:

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 item in food:
        if stock[item] > 0:
            total += prices[item]
    return total
    
print compute_bill(shopping_list)

Running it gives me the expected response but the following message:
Oops, try again. calling compute_bill with a list containing 2 apples, 2 pears, 2 oranges and 7 bananas resulted in 37.0 instead of the correct 33.0

When I add the next part of the exercise where we subtract the stock,

            stock[item] = stock[item] - 1

I get this message:
"Oops, try again. stock doesn't look quite right! Make sure to not call compute_bill since it changes the stock! It should contain: {'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}"

The first code you post is the result of trying code to comply with what error messages request.

Someone who can help us out of this problem.


#5

It looks like you need to use the above shopping_list. This latest code is from an earlier lesson, perhaps? Please post a link to the exercise. Thanks.


#6

In the exercise there are no values for the list, you copy what the exercise requests:

Instructions
Make the following changes to your compute_bill function:

While you loop through each item of food, only add the price of the item to total if the item's stock count is greater than zero.
If the item is in stock and after you add the price to the total, subtract one from the item's stock count.

?Hint
Get live, fast support from Codecademy Advisors when you get stuck.
Learn more.
If you're buying a banana, check if it's in stock (larger than zero). If it's in stock, add the cost of a banana to your bill. Finally, decrement the stock of bananas by one!


#7

Your opening post is confusing. Please post that link. Thanks.


#8

#9

Okay, for this one it looks like we just need the list, as your latter sample shows.

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

Your code actually looks fine. The problem might be that we are not asked to print anything, yet.


#10

Yes, that was, I went beyond what was asked, is to make me feel, THANK YOU


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.