CodeAJ
CodeAJ

Reputation: 110

How to find which products have the best value in Python

I am currently creating a code/application that will help shoppers determine which products to purchase, by calculating the best value. The best value of a product is calculated by finding the $/g, which is found by dividing the cost of the product by the mass of the product.

My problem is, that if there are multiple products that have the same best value, then it will only show one of the products that have the best value. Is there a way so that it will display ALL of the products with the best value? Thank you kindly in advance.

Here's my code so far:

def best_cost():
  while True:
    num_of_products = int(input('How many products are there? '))
    if num_of_products < 1:
      print(f'Enter valid number for number of products')
    else:
      print(f'Finding best value out of {num_of_products} products')
      all_prices = []
      for i in range(1, num_of_products+1):
        cost = float(input(f'Enter cost of product {i} $'))
        mass = float(input(f'Enter mass of product {i} in grams:'))
        print(f'Product {i} at ${cost / mass} per gram')
        price = cost/mass
        all_prices.append(price)
      for prod in all_prices:
        if prod == min(all_prices):
          best_prod = all_prices.index(prod)+1
          return print(f'Best product is Product {best_prod}')
best_cost()

Upvotes: 0

Views: 1040

Answers (2)

Chrispresso
Chrispresso

Reputation: 4071

The reason is because you return from the print statement, so you only ever get one item. You can instead build a list and return a list of items. You should also use something like math.isclose for comparing floating point values. Otherwise you can get some hard to track down errors.

You will also want to track the last index that you found a value for so you can find the next available item in the list.

from math import isclose

def best_cost():
  while True:
    num_of_products = int(input('How many products are there? '))
    if num_of_products < 1:
      print(f'Enter valid number for number of products')
    else:
      print(f'Finding best value out of {num_of_products} products')
      all_prices = []
      for i in range(1, num_of_products+1):
        cost = float(input(f'Enter cost of product {i} $'))
        mass = float(input(f'Enter mass of product {i} in grams:'))
        print(f'Product {i} at ${cost / mass} per gram')
        price = cost/mass
        all_prices.append(price)
      min_price = min(all_prices)  # You only need to do the min check once
      best_prods = []  # Create a list to store the results in
      for i, prod in enumerate(all_prices, 1):
        if isclose(prod, min_price):
          best_prods.append(i)
      print(f'Best product(s) are: {best_prods}')
best_cost()

Upvotes: 2

user6597761
user6597761

Reputation:

Your code works, but when you return the whole operation stops.

If you are not using the return value, you can not return and it will print all the new products

best_prod = all_prices.index(prod)+1
print(f'Best product is Product {best_prod}')

or if you want to return a list of all the best products, keep track of them with a list:

best_products = []
...
    for prod in all_prices:
      if prod == min(all_prices):
        best_prod = all_prices.index(prod)+1
        best_products.append(best_prod)
        print(f'Best product is Product {best_prod}')
return best_products

Upvotes: 1

Related Questions