【发布时间】:2022-01-18 12:08:06
【问题描述】:
所以我有一个包含一个对象的列表,但我想用它做一个搜索算法,这意味着如果单词相似,它将显示具有相似类别的项目。但是出现了错误。
代码:
print("\nHere are the results of your search")
if param == 'Electric Guitar':
results = inventory[item_006:item_010]
对象:
class Product:
def __init__(self, hproduct, htype, hprice, havail):
self.name = hproduct
self.type = htype
self.price = hprice
self.avail = havail
def __eq__(self,other):
return self.type == other
item_001 = Product('Tyma TD-10E Dreadnought', 'Acoustic Guitar', 23450, 'In Stock')
item_002 = Product('Baton Rouge AR21C/ME Traveler', 'Acoustic Guitar', 14900, 'In Stock')
item_003 = Product('Phoebus Baby 30 GS Mini', 'Acoustic Guitar', 6900, 'In Stock')
item_004 = Product('Maestro Project X X1-V1 OM', 'Acoustic Guitar', 32500, 'In Stock')
item_005 = Product('Sire A4 Grand Auditorium', 'Acoustic Guitar', 27490, 'In Stock')
item_006 = Product('Tagima TW55', 'Electric Guitar', 9500, 'In Stock')
item_007 = Product('Epiphone G400 ', 'Electric Guitar', 19500, 'In Stock')
item_008 = Product('D’Angelico Premiere DC', 'Electric Guitar', 49000, 'In Stock')
item_009 = Product('PRS Silver Sky', 'Electric Guitar', 138950, 'In Stock')
item_010 = Product('Vintage V100 Reissued', 'Electric Guitar', 27950, 'In Stock')
item_011 = Product('Phoebus Buddie 30 GS-E', 'Bass Guitar', 8720, 'In Stock')
item_012 = Product('Sire U5', 'Bass Guitar', 27490, 'In Stock')
item_013 = Product('Lakland Skyline Vintage J', 'Bass Guitar', 82950, 'In Stock')
item_014 = Product('Schecter Model T Session 5', 'Bass Guitar', 45900, 'In Stock')
item_015 = Product('Tagima Millenium Coda 4', 'Bass Guitar', 14900, 'In Stock')
item_016 = Product('Boss Katana 50 Mk II ', 'Accessory', 15950, 'In Stock')
item_017 = Product('TC Electronic BH250 Micro Bass', 'Accessory', 18990, 'In Stock')
item_018 = Product('Kemper Profiler Powerhead', 'Accessory', 130000, 'In Stock')
item_019 = Product('Headrush Pedal Board', 'Accessory', 27490, 'In Stock')
item_020 = Product('NUX MG30', 'Accessory', 12900, 'In Stock')
inventory = [item_001, item_002, item_003, item_004, item_005, item_006, item_007, item_008, item_009, item_010, item_011, item_012, item_013, item_014, item_015, item_016, item_017, item_018, item_019, item_020]
想要的输出:
Tagima TW55 Electric Guitar 9500 In Stock
Epiphone G400 Electric Guitar 19500 In Stock
D’Angelico Premiere DC Electric Guitar 49000 In Stock
PRS Silver Sky Electric Guitar 138950 In Stock
Vintage V100 Reissued Electric Guitar 27950 In Stock
错误:
slice indices must be integers or None or have an __index__ method
感谢您的任何提示和帮助。
【问题讨论】:
-
什么是
alist? -
存储对象的列表
-
我编辑了它@LeopardShark
-
inventory[5:10]?