13/13 What if i want to buy more than just one banana? Personal Challenge


#1

So this is a personal challenge for myself. How do I program it so that it would know i want to take 3 banana's instead of one? i can simply write more stings but that would be quite a hassle. Anyone got any ideas? thank you


#2

@ianleeyoung,

Change the design of the data so that the shopping list is a dictionary that specifies how many of each item to get. Do not allow the shopper to buy more of any item than there are of that item in stock.

# dictionary that specfies how many of each item to buy
shopping_list = {"banana": 3, "orange": 1, "apple": 1}

stock = {
    "banana": 6,
    "apple": 17,
    "orange": 32,
    "pear": 15
}
    
prices = {
    "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}

def compute_bill(food):
    total = 0
    for item in food:
        # Limit the number_purchased by the number of that item in stock
        number_purchased = min(stock[item], food[item])
        total += prices[item] * number_purchased
        stock[item] -= number_purchased
    return total
    
print "$%.2f" % compute_bill(shopping_list)

Output:

$15.50

#3

why this code not working?


#4

it works. i tried it myself and it came out perfectly


#5

yup, this code works. tried it myself. thank you very much


#6

message: "Oops, try again. You shouldn't need to write out any of the food products."
please tell me where i did wrong


#7

oh, sorry, I just mixed up the job.it's all because of the new interface))


#8

Try using the interpreter in 13/13 rather than 12/13. the interpreter in 13/13 will give you more leaway for customization. i didnt change much except add the edited function and turned my list into a dictionary