Shipping Charges calculator not producing the correct result

Question:

The following is a sample of a running program:

Welcome to the Fast Freight Shipping Company shipping rate program. 
This program is designed to take your package(s) weight in pounds and 
calculate how much it will cost to ship them based on the following rates.

    2 pounds or less = $1.10 per pound
    Over 2 pounds but not more than 6 pounds = $2.20 per pound
    Over 6 pounds but not more than 10 pounds = $3.70 per pound
    Over 10 pounds = $3.80 per pound
    
    Please enter your package weight in pounds: 1
    Your cost is: $ 1.10
    Would you like to ship another package <y/n>? y
    Please enter your package weight in pounds: 5
    Your cost is: $ 11.00
    Would you like to ship another package <y/n>? y
    Please enter your package weight in pounds: 52
    Your cost is: $ 197.60
    Would you like to ship another package <y/n>? y
    Please enter your package weight in pounds: 8
    Your cost is: $ 29.60
    Would you like to ship another package <y/n>? y
    Please enter your package weight in pounds: 3
    Your cost is: $ 6.60
    Would you like to ship another package <y/n>? t
    Invalid Input
    Would you like to ship another package <y/n>? y
    Please enter your package weight in pounds: 10
    Your cost is: $ 37.00
    Would you like to ship another package <y/n>? n
    Thank you for your purchase, your total charge is $ 282.90 
    Goodbye!

This is what I have so far:

charges = 0
answer = "yes"

while (answer == "yes"):
 packageWeight = eval (input ("Enter weight of package: "))
 answer = input ("Do you have more items to input? <yes/no>? ")

 if (packageWeight <=2):
     charges = (packageWeight * 1.10)
     print ("The cost is: $",charges)
 elif (packageWeight >2) and (packageWeight<=6):
     charges= (packageWeight * 2.20)
     print ("The cost is: $", charges)
 elif (packageWeight >6)and (packageWeight<=10):
     charges = (packageWeight * 3.70)
     print ("The cost is: $", charges)
 else :
     charges = (packageWeight * 3.80)
     print ("The cost is: $", charges)

message = "Thank you for your purchase, your total charge is $"
sum= charges+charges
message += format(sum, ',.2f')

print(message)
print("Goodbye!")

how would I find the sum of all charges? And how would I be able to have the second question come after the cost?

So it would display:

    Enter weight of package: 3
    The cost is: 6.60
    Do you have more items to input? yes/no
Asked By: kilo

||

Answers:

I did not quite understand this statement how would I find the sum of all charges, but I solved your second question.

Here is the code:

def get_charges(current_weight):
    if (current_weight <= 2):
        return (current_weight * 1.10)
    elif (current_weight > 2) and (current_weight <= 6):
        return (current_weight * 2.20)
    elif (current_weight > 6) and (current_weight <= 10):
        return (current_weight * 3.70)
    else:
        return (current_weight * 3.80)


charges = 0
answer = "yes"

while (answer == "yes"):
    # Eval is not recommended, use a float and round if needed.
    packageWeight = float(input("Enter weight of package: "))
    charges += get_charges(packageWeight)
    print("Current charges: ", charges)
    answer = input("Do you have more items to input? <yes/no>? ")


print(f"Thank you for your purchase, your total charge is ${charges}")
print("Goodbye!")

eval() is not recommended for security reasons, so I changed it to float().

Let me know if you have any questions

Answered By: 0xRyN
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.