Python select item from class list

So, first I try to create a class that contains the name, price and number of elements. Then I wanted to make a function that would subtract the amount sold to the buyer after they entered the amount they were buying, and then calculate the total price.

Now, to add on top of this, I'm trying to select a user from a list of items.
The problem is that it seems that I seem to be getting errors while the program starts the buy function.

class Retail:
def __init__(self, price, unitsOnHand, description):
    self.price = price
    self.unitsOnHand = unitsOnHand
    self.description = description
def buy (self):
    print ("How many are you buying?")
    quant = int(input("Amount:  "))
    unitsOnHand -= quant
    subto = price * quant
    total = subto * 1.08
    print ("There are now ", unitsOnHand, " left")
    print ("The total price is $", total)

box = Retail(4.95, 20, "Boxes")
    paper =Retail(1.99, 50, "Big Stacks of Paper")
staples =Retail(1.00, 200, "Staples")
ilist = (box, paper, staples)
print ("Which are you buying? ", [box.description, paper.description,    staples.description])
ioi = input("Please use the exact word name: ")
if ioi == 'box':
    Retail.buy(ilist[0])
elif ioi == 'paper':
    Retail.buy(ilist[1])
elif ioi == 'staples':
    Retail.buy(ilist[2])

The error I get when I tried to start it,

    Traceback (most recent call last):
  File "C:/Users/XXXXXX/XXXX/Code/Retailclass", line 22, in <module>
Retail.buy(ilist[0])
  File "C:/Users/XXXXXX/XXXX/Code/Retailclass", line 9, in buy
unitsOnHand -= quant
UnboundLocalError: local variable 'unitsOnHand' referenced before assignment

I assume that he does not see the values ​​that I have already assigned to this element, and if so, how can I get it?

+3
source share
1

, , , - buy, , . , Retail, () .

. , . ( ), :

class Retail(object):

    def __init__(self, price, unitsOnHand, description):
        self.price = price
        self.unitsOnHand = unitsOnHand
        self.description = description

    def buy(self):
        if self.unitsOnHand == 0:
            print('Sorry, we are all out of {} right now.'.format(self.description))
            return
        print("How many are you buying? We have {}".format(self.unitsOnHand))
        quant = int(input("Amount:  "))
        while quant > self.unitsOnHand:
            print('Sorry, we only have {} left'.format(self.unitsOnHand))
            quant = int(input("Amount:  "))
        self.unitsOnHand -= quant
        subto = self.price * quant
        total = subto * 1.08
        print("There are now {} left".format(self.unitsOnHand))
        print("The total price is ${}".format(total))
        return

stock = {}
stock['box'] = Retail(4.95, 20, "Boxes")
stock['paper'] = Retail(1.99, 50, "Big Stacks of Paper")
stock['staples'] = Retail(1.00, 200, "Staples")

print("Which are you buying? {}".format(','.join(stock.keys())))
ioi = input("Please use the exact word name: ")

while ioi not in stock:
   print("Sorry, we do not have any {} right now.".format(ioi))
   print("Which are you buying? {}".format(','.join(stock.keys())))
   ioi = input("Please use the exact word name: ")

stock[ioi].buy()
0

All Articles