Error in computing bill( Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in None instead of the correct 7)


#1

Oops, try again. calling compute_bill with a list containing 1 apple, 1 pear and 1 banana resulted in None instead of the correct 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(shopping_list):
total=0
for i in shopping_list:
total+= (prices[i]*stock[i])
print total


#2

hint**
no need to multiply with stock !


#3

Mltiplying with stock could also be an alternative. I`m unable to do using stock number decrement.


#4

Mltiplying with stock could also be an alternative. I`m unable to do using stock number decrement.

Can you explain how?


#5

banana stock(number) * price, will give us total banana cost!!


#6

We don't have to find to total price of fruits available in stock but
price of shopping_list = ["banana", "orange", "apple"] (3 fruits)


#7

Unable to understand

Error: Oops, try again. stock doesn't look quite right! Make sure to not call compute_bill since it changes the stock! It should contain: {'orange': 32, 'pear': 15, 'banana': 6, 'apple': 0}

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

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

Write your code below!

food=["banana","apple","pear"]

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


#8

Can you tell which exercise you currently doing ?
(link or name of the section ?)


#9

A Day at the Supermarket ...no 12. stocking out


#10

Edit: Found your mistake! Your problem is not with the decrementation but with your array,

rename it shopping_list, and put it as the first line of your code.

Then change this call to refer to it,


#11

Aren't
stock[i] -= 1 and stock[i]= stock[i]-1 same?


#13

I just tested and It worked!


#14

food=["banana","apple","pear"]

Your fruits list is not called food but shopping_list.
Try to change and do reply if it worked or not!

Also you don't need to test it by yourself as CC's editor will test for all possible inputs
so remove it ..

print compute_bill(food)


#15

My mistake, corrected myself here

his problem was that his array containing food values was after the dictionaries.


#16

Good point but as he defined food (global variable) list, It should have worked.
But even changing its name to shopping_list list,It does not print anything(gives error).
But when you create a completely separate list (of fruits) and pass it to compute_bill..it lets you pass.


#17

■■■■ man totally confused


#18

plz post the correct code


#19

can you please post your update version of your code?
also include the lesson link please

do the following

  • copy your code
  • paste it here
  • select the whole code
  • click on the above </>

#20

Posting answers are against the CC guidlines.

Post your code in formatted form..

or do these things..

1.Delete this line of code print compute_bill(food)
2.Also change your name of your food list to shopping_list


#21

without calling the function how will function run ????