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} (%): '))
You need to save the original prices first:
Then save them as
prices
, and iterate over each product: