12 Stocking out


#1



https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/2/3?curriculum_id=4f89dab3d788890003000096

The error I receive when running my script is along the lines of this quotation from the checker:
Oops, try again.
calling compute_bill with a list containing 1 apple and 9 bananas resulted in 12 instead of the correct 24


When I run the code, in python and in the checker, it seems to work just fine, although I've seen on this forum other ways to achieve the result (which may be much shorter), why is the checker suggesting this way isn't correct?


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


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

    new_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 key in prices:
            for item in food:
                if key == item:
                    if new_stock[key] * prices[key] > 0:
                        total += prices[key]
                        new_stock[key] -= 1
        return total
        print total

    print shopping_list
    go = compute_bill(shopping_list)
    print go 

Thanks in advance.

indent preformatted text by 4 spaces


#3

@anastasia.pezzotta,

Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

With Courtesy to stetim94
https://discuss.codecademy.com/t/im-having-a-problem-on-lesson-11-23/2307/3?u=leonhard.wettengmx.n

or even better use
= https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet


#4

Hi,

So I understand my code isn't the most efficient, and I've learned from my mistakes, however, it does do the job, my question was; why wasn't it correct when it still does the job?

My reason for this question is that I've just spent around 3 days roughly trying to work out why it wasn't correct, but because it did the job, I was hesitant to change it.


#5

@jon-w ,

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


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

new_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 key in prices:
        print("key-prices {0}".format(key))
        for item in food:
            print("\titem-food {0}".format(item))
            if key == item:
                print("\t\tkey-item {0}-{1}".format(item,key))
                if new_stock[key] * prices[key] > 0:
                    total += prices[key]
                    new_stock[key] -= 1
    print total
    # a =return= will return a result AND will EXIT the function !!!
    return total

print shopping_list
go = compute_bill(shopping_list)
print go

gives the OUTPUT

['banana', 'pear', 'apple']
key-prices orange
	item-food banana
	item-food pear
	item-food apple
key-prices pear
	item-food banana
	item-food pear
		key-item pear-pear
	item-food apple
key-prices banana
	item-food banana
		key-item banana-banana
	item-food pear
	item-food apple
key-prices apple
	item-food banana
	item-food pear
	item-food apple
		key-item apple-apple
7
7

================================================

The Instruction

  • 01 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.

  • 02 If the item is in stock and after you add the price to the total, subtract one from the item's stock count.
    You create your own new_stock dictionary, and then do your stock-substraction from this dictionary
    The course checker is checking the stock dictionary.......


#6

I see, thanks for explaining, as you can tell I'm a noob!

your explanation, (while I can understand, can be tedious and possibly annoying to have to write out) is very very much appreciated.

thanks again!

regards

Jon


#7

@jon-w,
Look at the DIFF

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

stock = {
    "banana": 7,
    "apple": 0,
    "orange": 33,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

# Write your code below!
def compute_bill(food):
    print("shopping_list => {0}".format(shopping_list))
    print("stock < old > => {0}".format(stock))
    total=0
    for item in food:
        print("item-food {0}".format(item))
        if stock[item] >0:
            print("\tstock[item-food] >0 {0}".format(item))
            total += prices[item]
            stock[item] -= 1
    print("stock < new > => {0}".format(stock))
    return total

compute_bill(shopping_list)

[OUTPUT]

shopping_list => ['banana', 'orange', 'apple']
stock < old > => {'orange': 33, 'pear': 15, 'banana': 7, 'apple': 0}
item-food banana
	stock[item-food] >0 banana
item-food orange
	stock[item-food] >0 orange
item-food apple
stock < new > => {'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}

#8