Stocking out


#1


12 . Stocking Out

I get the following 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}


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

compute_bill(shopping_list)


#2

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}

Make sure to not call compute_bill.

remove the last line


#3

that's done it, thank you.


#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

I receive the error: Oops, try again. calling compute_bill with a list containing 1 apple, 2 pears and 8 bananas resulted in 38 instead of the correct 30.

Any advice? (Disregard the lack of indentation. It seems that when you copy and paste directly from the code, it removes indentation)


#5

check out your line about stock[item] += 1

you should change it to -=1

You're ADDING to the stock each time.


#6

Holy ■■■■! Wow, I can't believe I overlooked THAT :joy: Thanks a million!


#7

dont call the compute_bill function.....delete the last line of code


#8

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

Getting the error calling compute_bill with a list containing 1 apple, 1 pear and 1 banana caused the following error: list indices must be integers, not dict

Can't figure out whats wrong


#9

I'm getting the following error message:

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

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

Anyone know whats wrong?


#10

I'm having similar issues, but I did notice you had stock[item] += -1
I believe it should be stock[item] -= 1

Otherwise, I'm still getting similar errors, so I'm not much help!


#11

check indentations, I had similar error with incorrect indentation for 'return'. Now it works


#12

why we are deleting this line?


#13

the instruction doesnt ask for it


#14

That works for me :slight_smile:

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


#15

Write your code below!

def compute_bill(food):
total = 0
for item in food:
if stock[item] > 0:
total +=prices[item]
stock[item] -= 1
print stock
return total #<== Indent so that (return total) is on the same line as (total = 0])


#16

I have a question: what does '+=' means?


#17

You right - it is python!!!
The space in the code is extremely important.
The site formats the message and removes all spaces :frowning:
It will not return total = 0, because it is replaced in сycle:

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


#18

It means:

a += b
equal:
a = a + b

a += 1
equal:
a = a +1

a -= 1
equal:
a = a - 1


#19

Not sure what's wrong with my code.. :sweat:
I got error message:
"Oops, try again. compute_bill(['apple']) returned None, did you remember to return the result?"

My code as below:
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[key]>0:
total = total + stock[key]*prices[key]
return total

print compute_bill("shopping_list")


#20

+= adds another value with the variable's value and assigns the new value to the variable.

x = 3
x += 2 # x = x + 2, the += is a shortcut for python, saying a few keystrokes
print x # x=3, x or 3 plus 2, the new x is = to 5, hence
5
-=, *=, /= does similar for subtraction, multiplication and division.