-1

I've been trying to figure out how to make a Best Price Finder where you can enter an infinite amount of items with their mass and price, and then pick the best value item (mass and price most worth it). Does anyone know how to make a code similar to this? Because it's been a struggle.

Thanks

  • 3
    Can you show your attempt so far? – Daniel Poh May 04 '20 at 02:22
  • while True: product1=float(input('Cost of the first product($): ')) product2=float(input('Cost of the second product($): ')) mass1=float(input('Mass of first product(grams): ')) mass2=float(input('Mass of second product(grams): ')) product11=product1/mass1 product22=product2/mass2 if product11 < product22: print('Product One is better value at', (product11),'$/g') elif product22 < product11: print('Product One and Product Two are the same price.') again=input("Would you like to compare some more items? ") if again == 'no': break print("Happy shopping!") – Kksucksatcode May 04 '20 at 04:28
  • Use the edit button found below the tags of your question and paste your code there please. It is hard to read code in the comments – Daniel Poh May 04 '20 at 10:42

1 Answers1

0

There are flaws in your program logic but here is my go at the question. My assumption is you want to find the item with the highest price to mass ratio. In my code I compare the value of the item given compared to the best value so far.

best_item = "None"
best_value = 0

while True:
    mass = input("Enter mass (Type done to finish): ")
    if mass == "done":
        break
    mass = float(mass)
    price = float(input("Enter price: "))
    value = price / mass
    if value > best_value:
        best_value = value
        best_item = "Mass: " + str(mass) + " Price: " + str(price) + " Value: " + str(value)
print("Best item: " + best_item)

Sample Run

Enter mass (Type done to finish): 1

Enter price: 1

Enter mass (Type done to finish): 1

Enter price: 2

Enter mass (Type done to finish): 2

Enter price: 1

Enter mass (Type done to finish): done

Best item: Mass: 1 Price: 2.0 Value: 2.0

Daniel Poh
  • 129
  • 1
  • 10