Discount on a price

320 Views Asked by At

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} (%): '))
2

There are 2 best solutions below

0
On

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]}")
1
On
  while True:
    products = int(input('How many products are there? '))
    if products < 1:
      print(f'Enter valid number for number of products')
    else:
      print(f'Finding best value out of {products} products')
      all_prices = []
      for i in range(1, 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 product in all_prices:
        if product == min(all_prices):
          best_product = all_prices.index(product)+1
          print(f'Best Product is Product {best_product}')
          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.")
            else
             print(f'Best product is Product {best_product}')
          for i in range(1, products+1):
            discount_1 = float(input(f'Discount for Product {i} (%): '))
            discount1 = (({cost}*discount_1)/100)
            print(f'Best product is Product {best_product}')
best_cost()```