The following is a sample of a running program:
JavaScript
x
32
32
1
Welcome to the Fast Freight Shipping Company shipping rate program.
2
This program is designed to take your package(s) weight in pounds and
3
calculate how much it will cost to ship them based on the following rates.
4
5
2 pounds or less = $1.10 per pound
6
Over 2 pounds but not more than 6 pounds = $2.20 per pound
7
Over 6 pounds but not more than 10 pounds = $3.70 per pound
8
Over 10 pounds = $3.80 per pound
9
10
Please enter your package weight in pounds: 1
11
Your cost is: $ 1.10
12
Would you like to ship another package <y/n>? y
13
Please enter your package weight in pounds: 5
14
Your cost is: $ 11.00
15
Would you like to ship another package <y/n>? y
16
Please enter your package weight in pounds: 52
17
Your cost is: $ 197.60
18
Would you like to ship another package <y/n>? y
19
Please enter your package weight in pounds: 8
20
Your cost is: $ 29.60
21
Would you like to ship another package <y/n>? y
22
Please enter your package weight in pounds: 3
23
Your cost is: $ 6.60
24
Would you like to ship another package <y/n>? t
25
Invalid Input
26
Would you like to ship another package <y/n>? y
27
Please enter your package weight in pounds: 10
28
Your cost is: $ 37.00
29
Would you like to ship another package <y/n>? n
30
Thank you for your purchase, your total charge is $ 282.90
31
Goodbye!
32
This is what I have so far:
JavaScript
1
27
27
1
charges = 0
2
answer = "yes"
3
4
while (answer == "yes"):
5
packageWeight = eval (input ("Enter weight of package: "))
6
answer = input ("Do you have more items to input? <yes/no>? ")
7
8
if (packageWeight <=2):
9
charges = (packageWeight * 1.10)
10
print ("The cost is: $",charges)
11
elif (packageWeight >2) and (packageWeight<=6):
12
charges= (packageWeight * 2.20)
13
print ("The cost is: $", charges)
14
elif (packageWeight >6)and (packageWeight<=10):
15
charges = (packageWeight * 3.70)
16
print ("The cost is: $", charges)
17
else :
18
charges = (packageWeight * 3.80)
19
print ("The cost is: $", charges)
20
21
message = "Thank you for your purchase, your total charge is $"
22
sum= charges+charges
23
message += format(sum, ',.2f')
24
25
print(message)
26
print("Goodbye!")
27
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:
JavaScript
1
4
1
Enter weight of package: 3
2
The cost is: 6.60
3
Do you have more items to input? yes/no
4
Advertisement
Answer
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:
JavaScript
1
25
25
1
def get_charges(current_weight):
2
if (current_weight <= 2):
3
return (current_weight * 1.10)
4
elif (current_weight > 2) and (current_weight <= 6):
5
return (current_weight * 2.20)
6
elif (current_weight > 6) and (current_weight <= 10):
7
return (current_weight * 3.70)
8
else:
9
return (current_weight * 3.80)
10
11
12
charges = 0
13
answer = "yes"
14
15
while (answer == "yes"):
16
# Eval is not recommended, use a float and round if needed.
17
packageWeight = float(input("Enter weight of package: "))
18
charges += get_charges(packageWeight)
19
print("Current charges: ", charges)
20
answer = input("Do you have more items to input? <yes/no>? ")
21
22
23
print(f"Thank you for your purchase, your total charge is ${charges}")
24
print("Goodbye!")
25
eval() is not recommended for security reasons, so I changed it to float().
Let me know if you have any questions