Error: A day in the super market


#1

Hi,
A have this problem, writing the code a get the error:
Oops, try again. compute_bill(['apple']) returned 0 instead of 2

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 m in food:
if (stock[m] > 0):
total += prices[m]
return total

I can not find the problem, I have declared in the if statement that the stock[m] should be > 0.
But it gave the this error.
Can someone help ? Thank you in advance :slight_smile:


#2

what is the name of the exercise? Seems you run ahead with your code, checking if m (item) is in stock, is later. Not yet, by running ahead you can get an error as you experienced


#3

The lesson is called Making a Purchase and because of this error I can not go ahead.
First, i get the message that the result should be integer, but now it is operating with strings. So I made a statement for check if the parameter is int an then to calculate them, beacause in the dictionary we have string(apple, orange etc.) and a value wick is integer.

So I made a ckeck that way: if m.isdigital(): , but then it gave me the following error: Oops, try again. compute_bill(['apple']) returned 0 instead of 2 So I do not know what to do.

And I checked in the forum to see if someone else has suck a problem, and try with the code that you see: def compute_bill (food):
total = 0
for m in food:
if (stock[m] > 0):
total += prices[m]
return total


#4

but in this exercise, you shouldn't check if m is in stock, you are running ahead, that is for stocking out (the next exercise)


#5

OK, so what is the solution. What my code should be in order not to have this error ?


#6

you should get rid of the if condition which checks if items are in stock


#7

i would first try to get rid of the () I think that they are not needed is should look like: if stock[m]>0:, if that doesn't work I would check you indentation. that's always a problem.


#8

That is not the issue at all.

As @stetim94 pointed out, the issue is that we are not supposed to do anything with stock until the next exercise.


#9

all ur supposed to do here is return the total


#10

Okay so basically, your code would work. However, at this point, Codeacademy doesn't want you factoring in stock yet. So just get rid of the lines in your for loop containing stock.

:grin:
Sorry if this has already been suggested.


#11

A post was split to a new topic: Error: day in supermarket


#12

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