Ask Question
21 February, 19:57

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.

+4
Answers (1)
  1. 21 February, 22:34
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers