10. A day at Supermarket


#1

Ok the problem is:
Oops, try again. It looks like your code did not print the correct total. #The total is = 0 :frowning:

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

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]
total = 0
key = prices[key] * stock[key]
print key
total += key

print total

What i must do??
thanks for answer !!


#2

I'm reading this from the bottom, looking for what you do with total

print total - seems reasonable, that's what they ask you to do.

total += key Uhm. What's the value of key? It'll be a string, right, and the total is supposed to be a number? If you want to end up with a number then this doesn't make much sense. What is it that you want to add to total, and how can that information be accessed?

BTW. Mind the indentation when you post code, you can't just leave it out, it won't mean the same thing any more.


#3

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

I had this code but apparently it gives the wrong output,, can anyone point out where I have made the errors? Thank you
I obviously indented properly so that's not an issue, its just the post de indents it so just try and see anything wrong with this code apart from the indentation.


#4

total = 0
prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}

for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]
if stock[key]>0:
value = prices[key]*stock[key]
total=total + value
print total

Ok in this way it work ... I hope you can help or at least it's right

Sorry for the indentation i don't understand how to change in the preview.


#5

thanks css jumper it did actually work when I correct an indentation error I made so thanks for your help anyway although I'm checking this after it already worked so saw afterwards nevertheless thanks for the help.