Reputation: 57
Im trying to create a code that searches an item from an object list, but it only checks the lower part of the list. If i try to search for the items in the lower part of the list, it gives good output, but when I try the higher part, it doesn't do the job.
code:
def searching(alist):
param = input("\nWhat are you looking for? ")
param = param.title()
if binary(param, alist):
print("\nHere are the results of your search")
if param == 'Electric Guitar':
results = [obj for obj in alist if obj.type == param]
results = bubble(results)
if param == 'Bass Guitar':
results = [obj for obj in alist if obj.type == param]
results = bubble(results)
if param == 'Acoustic Guitar':
results = [obj for obj in alist if obj.type == param]
results = bubble(results)
if param == 'Accessory':
results = [obj for obj in alist if obj.type == param]
results = bubble(results)#Modify this such that the covered indexes will be of the same type
view_inv(results)
else:
print("\nSorry, we don't have the item that you are looking for.")
main()
def binary(word, sett):
first = 0
last = len(sett) - 1
asd = False
global paramm
while first<=last and not asd:
mid = (first + last)//2
if sett[mid].eq(word):
return True
asd = True
else:
if sett[mid].gt(word):
last = mid - 1
else:
first = mid + 1
def bubble(collection):
my_list = list(collection)
for endnum in range(len(my_list)-1, 0, -1):
for i in range(endnum):
if my_list[i].__gt__(my_list[i+1]): #the one to check the different types of variable and compare
my_list[i], my_list[i+1] = my_list[i+1], my_list[i]
return my_list
class:
class Product:
def __init__(self, hproduct, htype, hprice, havail):
self.name = hproduct
self.type = htype
self.price = hprice
self.avail = havail
def view(self):
print(self.name.ljust(35), self.type.ljust(28), str(self.price).ljust(8), self.avail)
def __lt__(self,other):
if type(other) is str:
return self.type < other
else:
if param == 'name':
return self.name < other.name
if param == 'price':
return self.price < other.price
def __eq__(self,other):
return self.name == other.name
def __gt__(self,other):
return self.price > other.price
def eq(self,other):
return self.type == other
def gt(self,other):
return self.type > other
def lt(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]
desired output:
Phoebus Buddie 30 GS-E Bass Guitar 8720 In Stock
Tagima Millenium Coda 4 Bass Guitar 14900 In Stock
Sire U5 Bass Guitar 27490 In Stock
Schecter Model T Session 5 Bass Guitar 45900 In Stock
Lakland Skyline Vintage J Bass Guitar 82950 In Stock
output:
Sorry, we don't have the item that you are looking for.
Any help will really do.
Upvotes: 1
Views: 58
Reputation: 66371
The problem is that inventory
is not ordered by the type
, which is what your search is looking for - your first sett[mid].type
is 'Electric Guitar'
, so you never look to its right.
Upvotes: 1