12. Stocking out



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

i've looked at other codes on here but i'm still confused, please help me!

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


A couple of issues:

The first is this line:

if stock > 0:

You are checking if stock is greater than 0. stock is a dictionary, not the individual item you want, however. Logically python shouldn't let you ask if a non int (non number) is greater than an int. This is a silly quirk of python 2.x and you cannot do it in python 3.x.

From the python docs:

The operators <, >, ==, >=, <=, and != compare the values of two objects. The objects need not have the same type. If both are numbers, they are converted to a common type. Otherwise, objects of different types always compare unequal, and are ordered consistently but arbitrarily.

The last line is the key bit for us and is basically saying for us here is if you have two different types (dict and int for us), one will always be greater than other. There isn't any reasoning to which one is larger but that type will always be larger than the other. In our case, a dict will always be larger than an int, even if it is an empty dict and the value of the int was infinity. As a result this is always coming out as True.

To solve this think what you need to change so you aren't comparing the whole dictionary, but rather the value from it you want.

Your next issue is in this line:

stock[items] -= stock[items]

What are we being asked to do?

If the item is in stock and after you add the price to the total, subtract one from the item's stock count.

Ok, so we are being asked to subtract just one item. Think about what you are subtracting. If you are still having trouble seeing where you went wrong, try writing it out but instead of the variable stock[items], try putting in a real number.

I've tried not to just give you the answer, if you need more help, just ask.


ok, thank you!i'm working on it, but if i need help may i ask you again?


Feel free to ask me.

A little tip, a simple way to get an idea of what your code is doing is to put print statements before and/or after the part you want to know about. E.g.:

print stock[items]
stock[items] -= stock[items]
print stock[items]


jagking i'm still having trouble, can you please help me again? sorry


wait nevermind i got it thank you so much!!!


I´m tring to solve this, but the result it´s that the func change de value for -1.

can you help me, to understunt the logic befor this?


            total = total + precios[item]
        print total
        inventario[item] = -1
        print inventario


I'm sorry, I'm not 100% sure what you are asking. I can see English isn't your native language but if you could try explain what you would like to know or are having issues with again I will hopefully be able to help.


Srry my inglish.. hahah

Mi problem is:

When i use this formula:

inventario[item] = -1

Its change the value of the item, for "-1" but, what i need is to rest 1 to the current value of item.

i hope i expres my self now, if i dont please tell me.


Nevermind, i fix this haha

I don´t understunt why it´s didn´t work befor:

inventario[item] = inventario[item]-1

I make you and other answer, maby you can help me to understunt:

When i apply this function

def calcularFactura(comida): 
    total = 0 
    for item in comida: 
        if inventario[item] >0:
            total = total + precios[item]
            inventario[item] = inventario[item]-1
        print total
        print inventario
    return total

I have this result:

{'manzana': 0, 'pera': 15, 'banano': 5, 'naranja': 32}
{'manzana': 0, 'pera': 15, 'banano': 5, 'naranja': 31}
{'manzana': 0, 'pera': 15, 'banano': 5, 'naranja': 31}

How can i print only the final result? it`s that possible? or because is a loop they print all de result.

there is a way i can store al the answer to print the final result later?

{'manzana': 0, 'pera': 15, 'banano': 5, 'naranja': 31}


Ok. To print the final result make sure what you want to print is outside and after the for loop, that way it will only run once everything in the for loop is done.

If you want to the final result of the inventario then you just need to print/ return it at the end.


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