531,088 views
10 votes
10 votes
You plan to make a delicious meal and want to take the money you need to buy the ingredients. Fortunately you know in advance the price per pound of each ingredient as well as the exact amount you need. The program should read in the number of ingredients (up to a maximum of 10 ingredients), then for each ingredient the price per pound. Finally your program should read the weight necessary for the recipe (for each ingredient in the same order). Your program should calculate the total cost of these purchases, then display it with 6 decimal places.

Example There are 4 ingredients and they all have a different price per pound: 9.90, 5.50, 12.0, and 15.0. You must take 0.25 lbs of the first, 1.5 lbs of the second, 0.3 lbs of the third and 1 lb of the fourth. It will cost exactly $29.325000.

User Matt Gaunt
by
3.3k points

1 Answer

17 votes
17 votes

Answer:

In Python:

prices = []

pounds = []

print("Enter 0 to stop input")

for i in range(10):

pr = float(input("P rice: "+str(i+1)+": "))

pd = float(input("Pound: "+str(i+1)+": "))

if pr != 0 and pd != 0:

prices.append(pr)

pounds.append(pd)

else:

break

amount = 0

for i in range(len(pounds)):

amount+= (pounds[i]*prices[i])

print("Amount: $",amount)

Step-by-step explanation:

These initialize the prices and pounds lists

prices = []

pounds = []

This prompts the user to enter up to 10 items of press 0 to quit

print("Enter 0 to stop input")

This iterates through all inputs

for i in range(10):

This gets the price of each item

pr = float(input("P rice: "+str(i+1)+": "))

This gets the pound of each item

pd = float(input("Pound: "+str(i+1)+": "))

If price and pound are not 0

if pr != 0 and pd != 0:

The inputs are appended to their respective lists

prices.append(pr)

pounds.append(pd)

If otherwise, the loop is exited

else:

break

This initializes total to 0

amount = 0

This iterates through all inputs

for i in range(len(pounds)):

This multiplies each pound and each price and sum them up

amount+= (pounds[i]*prices[i])

The total is then printed

print("Amount: $",amount)

User RnMss
by
2.9k points