Ask Question
25 July, 09:07

Write a program in python that can compare the unit (perlb) cost of sugar sold in packages with different weights and prices. The program prompts the user to enter the weight and price of package 1, then does the same for package 2, and displays the results to indicate sugar in which package has a better price. It is assumed that the weight of all packages is measured in lb. The program should check to be sure that both the inputs of weight and price are both positive values.

+3
Answers (1)
  1. 25 July, 09:47
    0
    weight1 = float (input ("Enter the weight of first package: "))

    price1 = float (input ("Enter the price of first package: "))

    weight2 = float (input ("Enter the weight of second package: "))

    price2 = float (input ("Enter the price of second package: "))

    if weight1 > 0 and price1 > 0 and weight2 > 0 and price2 > 0:

    unit_cost1 = price1 / weight1

    unit_cost2 = price2 / weight2

    if unit_cost1 < unit_cost2:

    print ("Package 1 has a better price.")

    else:

    print ("Package 2 has a better price.")

    else:

    print ("All the entered values must be positive!")

    Explanation:

    *The code is in Python.

    Ask the user to enter the weight and the price of the packages

    Check if the all the values are greater than 0. If they are, calculate the unit price of the packages, divide the prices by weights. Then, compare the unit prices. The package with a smaller unit price has a better price.

    If all the entered values are not greater than 0, print a warning message
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a program in python that can compare the unit (perlb) cost of sugar sold in packages with different weights and prices. The program ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers