Help me with 12. Stocking out


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
12. Stocking out

<In what way does your code behave incorrectly? Include ALL error messages.>
Your code looks a bit off. See the console window for the error. Check the Hint if you need help (line 24)

<What do you expect to happen instead?>

```python

shopping_list = [“banana”, “orange”, “apple”, “pear”]

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

<do not remove the three backticks above>
Anyone can help me? i don't know why doesn't work...:/ ty

#2

Hi @codepro21817,

Look here:

They ask you to not call compute_bill since it changes the stock. Also, check where your return keyword is indented. :slight_smile: We want to get the full loop from our stock, and that all depends on where/how our return keyword is indented.


#3

Check the indentation. Remember that Python is fussy about it. Why? The indentation indicates the levels and what code belongs to what code block.

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

compute_bill(shopping_list)

Tip
Like you did with the total, you can do the same thing with the stock[key] using -= 1.


#4

Thanks for the answers, anyway i can’t pass it, this is my code

shopping_list = ["banana", "orange", "apple", "pear"]

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

obviusly, the return be after the loop.

The same error…(calling functiont and without it):

File “python”, line 25
return total
^
IndentationError: unexpected indent


#5

This means return total was unexpectedly indented, and should be on the same indentation level as one of your code blocks.

If you don’t want the full iteration, sure… but we do want the full iteration. So we should do the opposite of indent… do you know what that is?


#6

Look at the indentation of return total. Where does it line up?

The return should be after everything is done, meaning the if and the loop.


#7

OMGGGGG sorry bad paste… :smile:

the same error… i can’t remove the call function but nothing change.

obviusly, the return it’s after the condition if and loop for…

nownow
thanks for help me DUUUUUUUUdes


#8

In the previous instructions, they ask:

Finally, return the total.

It appears that you’re returning the parameter food (look at line 25), not what they asked above.

Also, check out what @hellofromtonya said about indentation. Indentation really matters. :grin:


#9

Problem 1

Look at the error you are getting in the terminal:

File “python”, line 25
return food
^
IndentationError: unexpected indent

The online editor is really fussy about indentation matching up to a 2 space indentation.

To fix the indentation problem, check out the answer that I gave here.

Problem 2

The second problem is what @intermediategamer indicated above. You don’t want to return food. You are computing the total bill. What do you want to return?


#10

yeah, ty all, i know that, i need to return the total!

resolved. the error was with the spaces after the if and for statement.
im a java programer, and i HATE the indent!

Will not happen again, i promise you!!


#11

In other languages, we get code braces to define the code blocks by wrapping them up.

Good job!


#13

Hello @zunaira.wasim,

Instead of posting for help in someone else’s topic, you can create a new topic like it’s stated in the Guidelines. Thank you! :slight_smile:


#14

Sorry created another new topic


#15

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