Skip to content

Instantly share code, notes, and snippets.

@stephengruppetta
Created June 22, 2023 17:27
Show Gist options
  • Save stephengruppetta/5939934720e9cc7d48458d0eecd9ea6c to your computer and use it in GitHub Desktop.
Save stephengruppetta/5939934720e9cc7d48458d0eecd9ea6c to your computer and use it in GitHub Desktop.
class Item:
def __init__(self, name, qty=0):
self.name = name
self.qty = qty
def __str__(self):
return f"{self.name} - {self.qty} left"
class VendingMachine:
def __init__(self):
self.items_on_sale = []
def __getitem__(self, idx):
if isinstance(idx, int):
return self.items_on_sale[idx]
raise TypeError("Index should be an integer")
def add_items(self, items):
"""
Add items to the vending machine
:param items: sequence of Item objects
"""
self.items_on_sale.extend(items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment