12/13 .Stocking Out


#1

https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/2/3?
curriculum_id=4f89dab3d788890003000096#

Error message:

Oops, try again. calling compute_bill with a list containing 6 bananas resulted in 4 instead of the correct 24

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

is there anything wrong with my code or my indent?

kindly teach me how to do correct indentation
thank you.


#2

On first glance, yes.


#3

which line ?
kindly explain
thanks


#4

This line need only occur when a price is accumulated. And it falls to us to use the correct syntax: -=.


#5

thank you,

i've seen some people write this code as this
can you explain what the difference

  total += prices[t] 
        stock[t] -= 1

#6

While the indentation is superfluous, the execution is within the same scope as the accumulation of total. Strange as it is, it falls within reason.

    total += prices[t] 
    stock[t] -= 1

#7

so this code still acceptable

if stock[t] > 0:
             total = prices[t] + total
        stock[t] -= 1

#8

Jagged code may be acceptable but it is a poor practice, especially with Python. Code should be written with care to ensure even alignment of blocks.


#9

i see,

so it always right to write a code is like this ? :
`
total += prices[t]
stock[t] -= 1
`

basically put the operation before = sign


#10

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