Home > Mobile >  list index out of range error in Python ( reading CSV file)
list index out of range error in Python ( reading CSV file)

Time:12-14

I am trying to create a shop in Python using a procedural approach rather than Object Oriented and then I created this function which reads my CSV file with the shop stock, except for the first row of the CSV file that contains the "cash available" in the shop.

I was trying to then access to price and quantity but I get the error:

p=(row[0], float(row[1]))
IndexError: list index out of range

This is my code for reference:

def createAndStockShop():
    s=Shop()
    with open("Stock.csv")as csv_file:#reading our stock file 
        csv_reader=csv.reader(csv_file,delimiter=',')#creating csv reader variable setting up delimiter as ","
        first_row=next(csv_reader)
        s.cash=(first_row[0])#extracting shop cash from first row of the file
        for row in csv_reader:#using for loop to go through the file 
            p=(row[0], float(row[1]))
            ps=ProductStock(p,float(row[2]))
            s.stock.append(ps)
            print(ps)
    return s#returning shop

And for reference this is how the file "Stock.csv" looks: csv file I am opening in with this code and containing my stock

In addition these are the classes I have created for Product stock and shop to give some more context:

@dataclass #Creating first data class for Products in shop
class Product:
    name: str #values are "name" as string and price as float to include decimals
    price: float = 0.0

@dataclass #Creating data class for the stock in shop
class ProductStock:
    product:Product
    quantity:int

@dataclass #Dataclass for shop, values cash as a float and stock as list
class Shop():
    cash: float = 0.0
    stock: List[ProductStock] = field(default_factory=list)

CodePudding user response:

Thank you so much guys, At the end I removed the empty lines in the file as you advised and it worked!! Thanks so much. However now is telling me a different error: ''' " line 126, in checking_stock if item.product.name == prod.product.name and item.quantity <= prod.quantity:#checking if item name in list matches the name in stock AttributeError: 'tuple' object has no attribute 'name'"

''' I understand it refers to this piece of code:

#defining method to check the shop stock: 
def checking_stock(c, s): #parameters for this function will be "c" ( customer) and s (shop)
    for item in c.shopping_list:#looping through the items in customer shopping list 
        for prod in s.stock:
                if item.product.name == prod.product.name and item.quantity <= prod.quantity:#checking if item name in list matches the name in stock
                    #also if quantity needed is less than the amount or product in stock
                    print(item, item.quantity, prod.quantity)#if this is the case, print item quantity and agree with the purchase
                    print("\nPerfect! you can proceed.")
                
                elif item.product.name == prod.product.name and item.quantity > prod.quantity:#else if the product amount requested by client is bigger than stock
                    print(f"Hey! So sorry! We do not have enough stock of: {item.product.name}, please select a different amount.")#printing error message
                    main()

As shown before the class I created for Product has an attribute name :

@dataclass #Creating first data class for Products in shop
class Product:
    name: str #values are "name" as string and price as float to include decimals
    price: float = 0.0

Is that related to this? Thank you so much.

CodePudding user response:

based on the image you have posted ... lines 9,10 are empty lines. Remove them and try again.

  • Related