Sal's Shipping Project Question

Hello, can anyone please tell me why my print commands are being ignored at the end of the cheap_ship function?:

#function that gives the cost of normal ground shipping 
def cost_of_ground(weight):
  if weight<=2:
    return 1.5*weight+20
  elif weight<=6:
    return 3*weight+20
  elif weight<=10:
    return 4*weight+20
    return 4.75*weight+20

#premium ground shipping

#function that gives the cost of drone shipping 
def cost_of_drone(weight):
  if weight<=2:
    return 4.5*weight+0
  elif weight<=6:
    return 9*weight+0
  elif weight<=10:
    return 12*weight+0
    return 14.25*weight+0

#tests functions and variables with print statements

#function that determines the cheapest way to ship
def cheap_ship(weight):
  if cost_of_ground(weight)<cost_of_pground and cost_of_ground(weight)<cost_of_drone(weight):
    return"Ground shipping is your best bet at a cost of $"+str(cost_of_ground(weight))+"."
  elif cost_of_drone(weight)<cost_of_pground and cost_of_ground(weight)>cost_of_drone(weight):
    return"Drone shipping is your best bet at a cost of $"+str(cost_of_drone(weight))+"."
  elif cost_of_drone(weight)>cost_of_pground and cost_of_ground(weight)>cost_of_pground:
    return"Premium ground shipping is your best bet at a cost of $"+str(cost_of_pground)+"."
    return"Two or more methods of shipping would work just as well for your needs."
  print("Thank you for using CheapShipTM!")
  print("Have a nice day!")
#tests my cheap ship function

Hi, @beanofdoom,

When a return statement is executed, what happens concerning the execution of the function that contains that statement?


Thanks for your response. I guess that the answer is that the return statement makes the function stop, but then this leaves me with two questions:

  1. Is it unnecessary then to use elif with return statements as long as the ‘if’ statements are in the right order? I though the benefit of using ‘elif’ over a bunch of ‘if’ statements was that it kept the function from executing them all in instances where more than of them are satisfied.

  2. Could you give me a hint about how get the code above to append my print statements to the strings returned by the cheap_ship function?

Thanks again!

1 Like

In the case of the cheap_ship function, you don’t need to use elif, because each of the conditional blocks is in the correct order and contains a return. In fact, you don’t need else either. The elif blocks can use else, instead. What is now an else header can be removed entirely, and the statement within it can be realigned accordingly.

At the beginning of the function, create a multi-line string containing all the text from the print statements, and assign that to a variable. Let’s assume it is called parting_message. Begin that multi-line string with a line break.

Then, you can do something like this:

    return"Ground shipping is your best bet at a cost of $"+str(cost_of_ground(weight))+"." + parting_message
1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.