'Discount on a price

So this is my code so far but at the end there i am trying to add a discount option. I am not sure how to add it to multiple products. Please let me know how i can improve because i am sort of new to coding.

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()
discount_question=input('Is there a discount on any of the products? ')
if 'yes' in discount_question:
  print("Enter in 0 if there isn't a discount on thet item.")
discount_a= float(input(f'Discount for product {a} (%): '))


Solution 1:[1]

You need to save the original prices first:

def best_cost():
    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:'))
            # Make an if function here to make sure mass is not 0.
            # Maybe force the user to enter a valid mass with a while True loop
            if mass != 0:
                price = cost/mass
                print(f'Product {i} at ${price} per gram')
                # Append list (price, product)
                all_prices.append([price, i])
            else:
                print("error, no mass")

        print(
            f'Best product is Product {min(all_prices,key=lambda x:x[0])[1]}')
        # Send back all prices
        return all_prices

Then save them as prices, and iterate over each product:

prices = best_cost()
discount_question = input(
    'Is there a discount on any of the products? (yes, no)\n> ')

if discount_question.strip().lower() == "yes":
    print("Enter discount% for aech product. No discount = 0")
    for i, j in enumerate(prices):
        # Unpack list
        price, product = j
        discount = float(input(f"{product}: {price}\n> %"))
        prices[i][0] = prices[i][0]*(1-(discount/100))
        print(f"    - New price of {prices[i][0]}")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Freddy Mcloughlan