Reputation: 1
Python program to calculate the cost of sending a parcel.
The below program is giving a name error when trying to print the total cost:
price_of_parcel = float(input(" enter the price of parcel in rands (R) : R "))
total_distance = float(input(" enter the total distance to the parcel destination in (km): km "))
parcel_route = input(" do you want it by Air or Freight? (Air / Freight): ").strip().lower()
parcel_insurance = input(" do u want full insurance or limited insurance? ( Full / Limited) : ").strip().lower()
parcel_nature = input(" do you want parcel as gift or as no gift? (Gift / No Gift) : ").strip().lower()
parcel_urgency = input( " do you want a standard or priority delivery? ( Priority / Standadard ) : ").strip().lower()
if parcel_route == "Air" :
p_route = total_distance * 0.36
totalcost = p_route
if parcel_route == " freight " :
p_route = total_distance * 0.25
totalcost = p_route
if parcel_insurance == " full " :
p_insurance = 50.00
totalcost = p_route + p_insurance
if parcel_insurance == " limited " :
p_insurance = 25.00
totalcost = p_route + p_insurance
if parcel_nature == " gift" :
p_gift = 15.00
totalcost = p_route + p_insurance + p_gift
if parcel_nature == " no gift " :
p_gift = 0
totalcost = p_route + p_insurance + p_gift
if parcel_urgency == " Priority" :
urgency = 50.00
totalcost = p_route + p_insurance + p_gift + urgency
if parcel_urgency == " standard " :
urgency = 20.00
totalcost = p_route + p_insurance + p_gift + urgency
if parcel_route == "air" and (parcel_insurance == " full" or "limited") and (parcel_nature == "gift" or "no gift") and (parcel_urgency == "priority" or "standard"):
totalcost = totalcost = p_route + p_insurance + p_gift + urgency
print( " the total cost for the parcel is : " + str(totalcost))
else:
if parcel_route == "freight" and (parcel_insurance == " full" or "limited") and (parcel_nature == "gift" or "no gift") and (parcel_urgency == "priority" or "standard"):
totalcost = p_route + p_insurance + p_gift + urgency
print( " the total cost for the parcel is : " + str( totalcost))
The name error states that the variable created within the if statement is not defined.
I have tried a few things but the code does not seem to work.
Upvotes: 0
Views: 52
Reputation: 990
You bug is here -
parcel_route = input(" do you want it by Air or Freight? (Air / Freight): ").strip().lower()
parcel_route
is a string in lower case and with no trailing or leading spaces. Because you have used strip() and lower()
But, in your if
clause you have capital letter and spaces too!
if parcel_route == "Air" :
p_route = total_distance * 0.36
totalcost = p_route
if parcel_route == " freight " :
p_route = total_distance * 0.25
totalcost = p_route
As a result, neither of the two if
block is executed and your p_route
variable is never set!
Upvotes: 1
Reputation: 13407
I would predefine all the variables at the top i.e.
p_route = 0
p_insurance = 0
p_gift = 0
urgency = 0
You probably don't hit some of the if's with your input, creating one, or more of these variables.
Ah, and also - put the above more into "if - elif - else", you don't need to check some of the conditions, once some earlier ones are satisfied...
For your ref: https://www.tutorialspoint.com/python/python_if_else.htm
Upvotes: 1