Answer:
<em>This program is written using python</em>
<em>It uses less comments (See explanation section for more explanation)</em>
<em>Also, see attachments for proper view of the source code</em>
<em>Program starts here</em>
<em></em>
#Prompt user for price of package 1
price1 = int(input("Enter Price 1: "))
while(price1 <= 0):
price1 = int(input("Enter Price 1: "))
#Prompt user for weight of package 1
weight1 = int(input("Enter Weight 1: "))
while(weight1 <= 0):
weight1 = int(input("Enter Weight 1: "))
#Calculate Unit of Package 1
unit1 = float(price1/weight1)
print("Unit cost of Package 1: "+str(unit1))
#Prompt user for price of package 2
price2 = int(input("Enter Price 2: "))
while(price2 <= 0):
price2 = int(input("Enter Price 2: "))
#Prompt user for weight of package 2
weight2 = int(input("Enter Weight 2: "))
while(weight2 <= 0):
weight2 = int(input("Enter Weight 2: "))
#Calculate Unit of Package 2
unit2 = float(price2/weight2)
print("Unit cost of Package 2: "+str(unit2))
#Compare units
if unit1 > unit2:
print("Package 1 has a better price")
elif unit1 == unit2:
print("Both Packages have the same price")
else:
print("Package 2 has a better price")
<em></em>
Explanation:
price1 = int(input("Enter Price 1: "))
-> This line prompts the user for price of package 1
The following while statement is executed until user inputs a value greater than 1 for price
while(price1 <= 0):
price1 = int(input("Enter Price 1: "))
weight1 = int(input("Enter Weight 1: "))
-> This line prompts the user for weight of package 1
The following while statement is executed until user inputs a value greater than 1 for weight
while(weight1 <= 0):
weight1 = int(input("Enter Weight 1: "))
unit1 = float(price1/weight1)
-> This line calculates the unit cost (per weight) of package 1
print("Unit cost of Package 1: "+str(unit1))
-> The unit cost of package 1 is printed using this print statement
price2 = int(input("Enter Price 2: "))
-> This line prompts the user for price of package 2
The following while statement is executed until user inputs a value greater than 1 for price
while(price2 <= 0):
price2 = int(input("Enter Price 2: "))
weight2 = int(input("Enter Weight 2: "))
-> This line prompts the user for weight of package 2
The following while statement is executed until user inputs a value greater than 1 for weight
while(weight2 <= 0):
weight2 = int(input("Enter Weight 2: "))
unit2 = float(price2/weight)
-> This line calculates the unit cost (per weight) of package 2
print("Unit cost of Package 2: "+str(unit2))
-> The unit cost of package 2 is printed using this print statement
The following if statements compares and prints which package has a better unit cost
- <em>If unit cost of package 1 is greater than that of package 2, then package 1 has a better price</em>
- <em>If unit cost of both packages are equal then they both have the same price</em>
- <em>If unit cost of package 2 is greater than that of package 1, then package 2 has a better price</em>
if unit1 > unit2:
print("Package 1 has a better price")
elif unit1 == unit2:
print("Both Packages have the same price")
else:
print("Package 2 has a better price")