This is my second and last attempt to get this problem right. My first submissio
ID: 671334 • Letter: T
Question
This is my second and last attempt to get this problem right. My first submission was answered with a very unhelpful answer that did not even work in IDLE python 3.4. The person who responded was not at all helpful or close to getting the right answer. Please read the question and answer thoughtfully. I am really trying hard to understand this material. We just started if, if-else, and if-elif-else statements Please test your answer before submitting and make sure it works. Thanks for your assistance.
Write a program that asks the user to enter the weight of a package and then displays the shipping charges. This is directly from the book listed below.
Explanation / Answer
if-else statement is used when you want to perform different actions under different circumstances.
use if to execute a block of code when a specified condition is true.
use else if or elif (short form of else if) to execute a block of code when the before specified condition is false but a new condition is true.
else is used to execute block of code when all the before mentioned conditions are false
In the present scenario you want to use different rates based on different weights of the package.
-------
print("Fast Freight Shipping Company")
# promt the user to enter weight of the package, this is collected in variable 'weight'
weight = input("Enter Weight of a package: ")
# variable 'charges' which will hold the calculated shipping charges is declared and initialized to zero
charges = 0
# if the weight of the package is more than zero and less than or equal to 2
if (weight>0 and weight<=2):
charges = weight * 1.5
# or if the weight of the package is more than 2 and less than or equal to 6
if(weight > 2 and weight<=6):
charges = weight * 3
# or if the weight of the package is more than 6 and less than or equal to 10
elif ((weight > 6 and weight<=10)):
charges = weight * 4
elif (weight > 10):
charges = weight * 4.75
# if a negative number is input as weight, prompt the user to enter a positive number
else : print ("Please input a positive number as weight");
# charges are printed only when a positive number is input as weight and charges are calculated
if (charges > 0) :
print "Total Shipping Charges: ", charges
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.