A Day at the Supermarket: Why is this not working?


#1

why is this not working?
why does it calculate wrong?

image


A day at the supermarket not adding up
Why does it ask me not to use compute_bill?
#2

Hi @methodninja84657

Please post the code that creates your stock and prices dictionaries.

Did you change any of the values in the stock dictionary? It should contain …

    "banana": 6,

#3

I didn’t change. it was identical to the previous exercise.

the only difference is in lines 22,23. In the solution it appeared:
total += price[item]
stock -= 1


#4

I believe what @appylpye means is that they would need to be able to reproduce it in order to have a chance at figuring out what’s wrong.

I can’t spot it from what you’ve said, which makes me think the problem isn’t in what you’re showing, it’s somewhere else, and being able to reproduce it makes it much much easier to start narrowing down what it could possibly be.

It could very well be in what you’re showing, but I don’t want to look too hard without knowing that it is the case (which reproducing it could show me)


#5

here is the full code


#6

That requires an image-to-text conversion program.


#7

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

print compute_bill(shopping_list)

#8

What is the variable toal doing here?

      total = toal + prices[item]

#9

Still one missing piece.
Which exercise?
Some digging tells me:
https://www.codecademy.com/courses/learn-python/lessons/a-day-at-the-supermarket/exercises/stocking-out-
That code passes there. Can’t reproduce.

One explanation for the error message is that stock wasn’t checked (like in the code of previous exercise).
Maybe that is the right exercise (it’s the only one with that particular error message in it), but some other code than was shown was used, at least from the exercise test’s perspective


#10

Yes, the original compute_bill function looks fine, so the problem must have been elsewhere. But now, in the most recently posted version of the function, we have this variable toal.

Since the code has been modified several times since the original post, which did not show all the code, we may no longer be able to identify the original problem.


#11

i guess it was just that typo… the following code (identical to the first one but no typo) gave me the result of 5.5

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

print compute_bill(shopping_list)


#12

It’s not that typo. Your first picture doesn’t have it.

I think the code for the previous exercise is being used. Whether that’s codecademy or you mixing them up I can’t tell. I’d refresh the page, and if that doesn’t help, reset the exercise (help button in the corner)

Oh and keep in mind that practically all forums apply formatting to posts. (Try coping from your post, won’t work, it’s not an exact copy)


#13

I refreshed and copied this code again (notice the bold area) and it works. i dont know wht didnt it work the first time (same code)

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

print compute_bill(shopping_list)


#14

@methodninja84657,

The original message from Codecademy’s testing, which was …

calling compute_bill with a list of 2 pears, 2 oranges and 9 bananas resulted in 45.0 instead of the correct 33.0

… suggests that for the exercise that you were doing, your original code managed to sell 9 bananas, when there should have been only 6 bananas in stock. We may no longer be able to determine how that happened.

Please let us know when you are able to submit a successful solution, and make sure you understand how and why it works.