A Day at the Supermarket...Gone Wrong


#1



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


Oops, try again. calling compute_bill with a list containing 2 apples, 3 pears and 9 bananas resulted in 45 instead of the correct 33


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


#2

This expression does not result in any change to stock. It should be a statement...

 stock[x] -= 1

#3

where are you passing the contents of shopping_list to the argument food in compute_bill(food) ?


#4

Even changing the stock[x] - 1 to stock[x] -= 1 doesn't get the math right for even 1 item each from the shopping_list. The site tells me the answer is 2, but my code got 10.5, or 7.5.... my pencil comes up with 7.5, but my code comes up with 5.5. What am I missing?


#5

The amount 5.5 is correct for 1 banana, 1 orange, 1 apple, but we cannot call the compute_bill function ourselves else it alters the inventory before the lesson checker runs it. Your code is fine (though it does not need the else branch).


#6

banana = 4
apple = 2
orange = 1.5

That sum should be 7.5, not 5.5


#7

In any case, even if my code would run in a terminal running python, it
won't run in the tutorial, so I can progress no further than this
exercise. And now it seems I need to move to Python 3 with different
nuances (I'm told) in syntax....just been a frustrating day of one thing
after another.


#8

5.5 is correct. Recall there are not apples in stock. The amount is not important in this lesson. Do not call the function.


#9

Not in this track, you won't. It is Python 2 through to the end. If your professor is wanting you to move on to Python 3, the bulk of the material in this course is still valid. The nuances that stick out are,

print  =>  print()

print range(5)  =>  print (list(range(5))

1 / 2 == 0  =>  1 / 2 == 0.5

raw_input()  =>  input()

The following page breaks down the main differences...

http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html


#10

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