0

I create some code to simulate a vending machine, I want to know what is in the machine, but I do not manage to get there.

This is my output when I print(machine):

[<__main__.Vending_slot object at 0x7fd59f53db38>,
 <__main__.Vending_slot object at 0x7fd59f53dba8>,
 <__main__.Vending_slot object at 0x7fd59f53db00>,
 <__main__.Vending_slot object at 0x7fd59f53dbe0>]

Code:

class Vending_slot:

    def __init__(self, product, stock, price):
        self.product = product
        self.stock = stock
        self.price = price

    def dispense_item(self):
        print(f"You bought a {self.items[0]}.")
        self.items = items[1] 


machine = []
machine.append(Vending_slot("A1", ["Coke","Coke","Coke"], 1.5))
machine.append(Vending_slot("A2", ["Water","Water","Water"], 0.5))
machine.append(Vending_slot("B1", ["Twix","Twix","Twix"], 1))
machine.append(Vending_slot("B2", ["Gum","Gum","Gum"], 0.8))

print(machine)
Prune
  • 76,765
  • 14
  • 60
  • 81
alladin
  • 33
  • 3
  • 1
    In particular, what do you *expect* to see? You appended four vending slot objects to a list, and then printed out the list of objects. That's what you have: four object handles. You have no code that reaches into the objects to print out the attributes. Show us what you want to print, and perhaps take a shot at coding that part. For instance, a method `display_slot` that prints the contents might be a good addition to your class. – Prune May 08 '19 at 22:53

1 Answers1

1

You need to implement __repr__ method to see attributes of Vendind_slot when using print. Add this to your class:

def __repr__(self):
    return f"{self.product} {self.stock} {self.price}"

Output of print(machine) will be:

[A1 ['Coke', 'Coke', 'Coke'] 1.5,
 A2 ['Water', 'Water', 'Water'] 0.5,
 B1 ['Twix', 'Twix', 'Twix'] 1,
 B2 ['Gum', 'Gum', 'Gum'] 0.8]
sanyassh
  • 8,100
  • 13
  • 36
  • 70