-1

I have three parallel arrays product, store and price. The program should ask for the product and store name and then display all the prices against that product and store. I have the following code but it's stopped displaying price once it's found the first value in the sorted price list. Is there any way to search for more than one value from the list? Here is my code.

product = ["Milk","Peanut","Onions","Milk","Onions","Cheese","Potatoes","Cheese"]
store = ["Axep","Dominion","Fortions","Axep","Extra Foods","Loblow","Axep","Loblow"]
price = [2,2,4,10,10,10,17,23]

def search(product,store,price):

  enterProduct = input("Enter product: ")
  enterStore = input("Enter store: ")

  counter = 0
  position = 0
  found = False

  while counter < len(price) and found == False:
    if product[counter] == enterProduct and store[counter] == enterStore:
      found = True
      position = counter

    counter = counter + 1
  
    if found == True:
      print(f"The price for {product[position]} in {store[position]} are {price[position]}")

def main():

  search(product,store,price)

main()

Here is the output:

Enter product: Milk
Enter store: Axep
The price for Milk in Axep are 2

which should be:

The price for Milk in Axep are:
2
10
John
  • 55
  • 4

3 Answers3

1

There is no need to use the found flag at all. Just print right inside the if block:

counter = 0

while counter < len(price):
    if product[counter] == enterProduct and store[counter] == enterStore:
        print(f"The price for {product[counter]} in {store[counter]} are {price[counter]}")

    counter = counter + 1

But using a while loop like that is quite "unpythonic". It would be better to use the zip function to iterate over multiple lists at once:

products = ["Milk", "Peanut", "Onions", "Milk", "Onions", "Cheese", "Potatoes", "Cheese"]
stores = ["Axep", "Dominion", "Fortions", "Axep", "Extra Foods", "Loblow", "Axep", "Loblow"]
prices = [2, 2, 4, 10, 10, 10, 17, 23]

enterProduct = input("Enter product: ")
enterStore = input("Enter store: ")

for product, store, price in zip(products, stores, prices):
    if product == enterProduct and store == enterStore:
        print(f"The price for {product} in {store} are {price}")
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Thanks. It's work but do you think we no more use a Linear Search in the solution? – John Feb 13 '22 at 16:07
  • 1
    @John That's still a linear search. It iterates over all lists one element at a time, in order. It just does it to all three lists in parallel. It is exactly equivalent to your `while` loop, just more "pythonic"... – Tomerikoo Feb 13 '22 at 16:58
-1

Set the found variable to False inside the if clause.

if found == True:
  print(f"The price for {product[position]} in {store[position]} are {price[position]}")
  found = False

Explanation: After finding an element you are setting the found variable to True. But you are never setting it back to False. Thus in the subsequent iteration, the while loop's condition will evaluate to false.

while counter < len(price) and found == False:

Thus it will not enter the while loop.

Note: You don't need to use the found variable. You can print the search result inside the first if clause like this:

while counter < len(price):
if product[counter] == enterProduct and store[counter] == enterStore:
  print(f"The price for {product[counter]} in {store[counter]} are {price[counter]}")
  # or insert the counter to another list for postprocessing.
mahbub
  • 29
  • 4
-1

You could use this code to get the exact functionality you want:

product = ["Milk", "Peanut", "Onions", "Milk", "Onions", "Cheese", "Potatoes", "Cheese"]
store = ["Axep", "Dominion", "Fortions", "Axep", "Extra Foods", "Loblow", "Axep", "Loblow"]
price = [2, 2, 4, 10, 10, 10, 17, 23]

pr = input("Enter product: ")
st = input("Enter store: ")
print(f"The price for {pr} in {st} are:")
[print(m) for (p, s, m) in zip(product, store, price) if (pr, st) == (p, s)]

output:

Enter product: Milk
Enter store: Axep
The price for Milk in Axep are:
2
10

You could also look into pandas DataFrames as a overall better solution. official docs GeeksForGeeks

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • [Don't use list comprehensions for side effects](https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects). If you want to print - use a loop. – Tomerikoo Feb 14 '22 at 16:16