Several days of effort has not yielded a sloution


#1

been stuck for a several days on this, cant firgure what's going on. gives back the error code "Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: unhashable type: 'list'" can somebody show what the correct code looks like?

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


#2

nope, they can not. Its a violation of the guidelines

there are several problems with your code:

new = stock[food]

Could you explain why you wrote that line of code?

here:

total += prices[food]

food is a list, you can't a list as key for dictionary

same here:

new += stock[food]

you have a for loop here:

for item in food:

now item will (in order/sequence) contain each value of the food list, which you can use as key to get the price and add to total and reduce the stock of item


#3

creating "new" was an effort to fix the problem of not having a modifiable variable for stock, I'm somwhat confused as to the difference between = and +=. for the following code I get this error

Traceback (most recent call last):
  File "python", line 25, in <module>
  File "python", line 21, in compute_bill
TypeError: unhashable type: 'list'

# Write your code below!
def compute_bill(food):
    total = 0
    for item in food:
        total += prices[food]
        new += stock[food]
    return total
    
print compute_bill(shopping_list)

#4

i hate to bring bad news, but that won't work

here:

total += prices[food]

food is a list, lists can't be used as dictionary key. As you see, you will get a TypeError (list is not a valid type for dictionary key)


#5

is this closer?
err msg:
calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: local variable 'stock' referenced before assignment

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

#6

revised code
err msg:
calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in None instead of the correct 7

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

#7

that is a weird error message, stock should be present:

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

this:

total = prices[i] + total

is now good, well done :slight_smile:

now here:

stock = stock[food] - 1

you want to reduce the current the stock of the current item (stock[i]) by one, this means that stock[i] is at both sides of the equal sign


#8

get rid of else:
return
worked for me


#9

good point, didn't see that

return will end the function, so the function now ends the moment something is not in stock, while the might be items left in list the customer wants to buy, so we won't wnat the function to end


#10

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