[Fixed] 12. Stocking out. Problem with multiple fruits of the same sort


#1



Hee guys,

I am stuck at "A Day at the Supermarket"
12.Stocking Out
https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/3

This is the error message I get:

Oops, try again. calling compute_bill with a list containing 2 oranges and 8 bananas resulted in 5.5 instead of the correct 27.0

I know it has to be something with the multiplication of several times the same fruit, but I do not know how to fix it.

Thanks for the help!
xx


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 = total + prices[item]
            stock[item] = stock[item] =-1
    return total


#2

syntax problem

total = total + prices[item]
stock[item] **=** stock[item] **=-1**

double equal operator.
change this to

total += prices[item]
stock[item] -= 1

this.


#3

I changed it, but it still gives the same error:

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

I don't know how to post my new code in the pretty formula they gave me in my initial post


#4

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]
            stock[item] -= 1
        return total

#5

Hi now its just a indent problem your return statemnt should be like that

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

#6

That is such a silly mistake! Thank you so much for helping me.
Sorry for wasting your time, very thankful though :slight_smile:


#7

But even with the return command belonging to the for loop, the returned value should be the same, no?


#8

@psysc0rpi0n- The returned value won't be the same, as no sooner it encounters the first return statement, it will exit the for loop [with a wrong value], and come to the calling statement.


#9

Hi, i just tried this and i keep getting this error message.

" Oops, try again. compute_bill(['apple']) returned 0 instead of 2 " . since there is no stock , there wont be a bill associated with it .


#10

What exercise are you on @asher3667. Can you please post a link to it? Thanks!


#11

I am on the "Day at a supermarket " exercise 11 . My code is the same as the ones before this post. . I'll post it anyways: shopping_list = ["banana", "orange", "apple"]

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

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

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


#12

** with the proper indentation

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


#13

Your problem is that in this part of the exercise you do not need to check whether an item is or isn't in stock. Therefore, because your existing code checks whether or not an item is in stock, it is giving an error. I suggest you copy the code and fix it, and you can just paste this code for the next lesson, where you actually need to check whether or not an item is in stock.

Hope this clears out your problems...


#14

I understand what you're saying, but shouldn't there be a way of being able to omit if any product isnt in stock ? so naturally there is no need for it to be shown in the output or just plain zero as its value. If this is going to be covered in the lessons yet to come, then forgive me. I will proceed onto the next exercise.


#15

Yes, and that is the aim of the exercise directly after this...


#16

oh my bad. Yes I just went through the next exercise .Sorry for that. And Thankyou for everything !


#17

No worries, @asher3667. If you feel my replies are satisfactory, please leave one of them as the solution to notify others that this topic is closed. Keep coding hard :laughing:!