12/13 iteration problem


#1

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! total = 0

for item in stock:
number_purchased = min(stock[item])
total += prices[item] * number_purchased
stock[item] -= number_purchased

It returns Traceback (most recent call last):

File "python", line 19[ number_purchased = min(stock[item]) ] , in
TypeError: 'int' object is not iterable.
Somebody help please.


12-Stocking out
#4

Hi @aliyu123,

Here's a little trick:

Whenever you would like to paste code, since this forum supports Markdown you can use three backticks before and after your code block like so:

```
def my_func(x):
my_var = 0
if x > my_var:
sum = my_var + x
else:
print "Odelay!"
return sum
```

and it will retain proper indentation and even have nifty syntax highlighting:

def my_func(x):
    my_var = 0
    if x > my_var:
      sum = my_var + x
    else:
      print "Odelay!"
    return sum

That being said, let's look at your code. When I indent it like so:

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 stock:
        number_purchased = min(stock[item])
        total += prices[item] * number_purchased
        stock[item] -= number_purchased

print compute_bill(shopping_list)

I get the following error as well:

Traceback (most recent call last):
File "", line 26, in
File "", line 22, in compute_bill
TypeError: 'int' object is not iterable

Before we go anywhere with that error message, what I see immediately is that you are trying something else than what the instructions are telling you to do...

Instructions:

  1. While you loop through each item of food, only add the price of the item to total if the item's stock count is greater than zero.
  2. If the item is in stock and after you add the price to the total, subtract one from the item's stock count.

This should translate to something like that in code:

for item in food:            # While you loop through each item of food
    if stock[item] > 0:      # if the item's stock count is greater than zero
        total += prices[item]    # add the price of the item to the total
        stock[item] -= 1         # substract one from the item's stock count

Make sense? :smile:

Let's add a few print statements to help us understand why it make sense:

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:

        print "The number of %s in stock is %s" % (item, stock[item])

        if stock[item] > 0:

            print "  Processing one %s priced at %s $ ..." % (item, prices[item])
            print "    Adding %s $ to the total" % (prices[item])

            total += prices[item]

            print "    Removing one %s from the stock" % (item)

            stock[item] -= 1

            print "    Total is now %s and the amount of %s in stock is now %s" % (total, item, stock[item])
    return total

print compute_bill(shopping_list)

Hope this helped! :smile: