Python3 Class Course Help

This is from the classes python3 course. I can include the whole code if thats helpful, but I am just trying to understand why it works. Where is it signifiing to add the values into the bill? New to python, but I assumed you would do something like bill += self.items[values]. Don’t know if that question makes sense so happy to ellaborate more if needed

  def calculate_bill(self, purchased_items):
    bill = 0
    for purchased_item in purchased_items:
      if purchased_item in self.items:
        bill += self.items[purchased_item]
    return bill


brunch_items = {
  'pancakes': 7.50, 'waffles': 9.00, 'burger': 11.00, 'home fries': 4.50, 'coffee': 1.50, 'espresso': 3.00, 'tea': 1.00, 'mimosa': 10.50, 'orange juice': 3.50
}
brunch_menu = Menu('Brunch', brunch_items, 1100, 1600)

Hi,
It looks like it’s using a dictionary for the items. So, each entry (key) will have a value associated with it. (Also known as a key-value pair)
The loop is running through the items in purchased_items and adding the value associated to it to bill.

E.g.
in brunch_items, ‘pancakes’ is the first key, and 7.50 as its associated value.
so,
n = brunch_items[‘pancakes’]
would mean
n = 7.50

Hope that helps

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