我试图编写一个代码来显示每个项目的可用库存(stock_level)。但是我收到了标题中提到的错误。我将txt文件导入数组code,description,stock_level,min_stock,max_stock,buying_price,selling_price1,tukul,3,5,25,20.00,25.002,mouse,20,5,25,30.00,35.003,keyboard,2,5,25,30.00,35.004,headphone,20,5,25,100.00,105.005,monitor,1,5,25,150.00,155.006,cpu,20,5,25,700.00,705.007,casing,20,5,25,100.00,105.00到目前为止,这是我的代码:import numpy as npimport csvlist = np.zeros((7,8),dtype = str)a = np.genfromtxt('stock.txt',delimiter = ',',dtype = str)#print(a)class stock: def __init__(self, code, description, stock_level, min_stock, max_stock, buying_price, selling_price): self.code = code self.description = description self.stock_level = stock_level self.min_stock = min_stock self.max_stock = max_stock self.buying_price = buying_price self.selling_price = selling_price def sell(stock): i = 0 while i < 7: for r in a: r = 1 for c in a: print(a[r][1]," have ", a[r][2]," of stocks") r += 1 i += 1disp = stock("","",0,0,0,0,0)disp.display()#disp.buy()#disp.buy()disp.sell()问题出在函数 sell() 中这是我得到的输出:tukul have 3 of stocksmouse have 20 of stockskeyboard have 2 of stocksheadphone have 20 of stocksmonitor have 1 of stockscpu have 20 of stockscasing have 20 of stocksTraceback (most recent call last): File "D:\OOP\Python\Kiosk\main.py", line 72, in <module> disp.sell() File "D:\OOP\Python\Kiosk\main.py", line 64, in sell print(a[r+1][1]," have ", a[r+1][2]," of stocks")IndexError: index 8 is out of bounds for axis 0 with size 8>>我确实得到了我想要每个项目可用的库存清单的结果。但我不知道是什么导致了错误
添加回答
举报
0/150
提交
取消