Sal's Shipping - How to Improve My Code

I successfully completed the Sal’s Shipping challenge and then watched the video of the developer doing the code in order to see if he approached it more efficiently (he did). However, it was a bit frustrating because his answer relied on material we haven’t learned yet. As a former teacher, I don’t see the usefulness in showing an answer that relies on material that students have never seen. These tools ($%.2f and %s and how to combine them with variables) haven’t been taught as of this Sal’s Shipping challenge, so I wasn’t able to get much information on how to condense my code in the last part of the challenge.

Watching the video, I did learn/remember that I don’t need to repeat information covered in the first “if” clause (so, for example, I didn’t need to write:

elif weight > 6 and weight <= 10

I could’ve just written:

elif weight <=10

So in that respect, the video was helpful. I’d also like to note that I know that some of the mathematical parentheses aren’t necessary because of order of operations, but I preferred to include them to remind myself of what was happening (that the drone price was 3x the ground price). But thanks in advance to anyone who has any tips on how to improve the final (print) section of my code without using the two tools that we haven’t learned yet.

Without further ado:

flat_ground_charge = 20
def ground_ship_cost(weight):
  if weight <= 2: 
    cost = weight * 1.50 
  elif weight > 2 and weight <= 6:
    cost = weight * 3 
  elif weight > 6 and weight <= 10:
    cost = weight * 4 
  else:
    cost = weight * 4.75 
  return cost + flat_ground_charge
  

print(ground_ship_cost(8.4))

premium_ground_ship_cost = 125

def drone_ship_cost(weight):
  if weight <= 2: 
    cost = weight * (1.50*3) 
  elif weight > 2 or weight <= 6:
    cost = weight * (3.00*3) 
  elif weight > 6 or weight <= 10:
    cost = weight * (4.00*3) 
  else:
    cost = weight * (4.75*3) 
  return cost 

print(drone_ship_cost(1.5))

def calc_cheapest_option(weight):
  ground_price = ground_ship_cost(weight)
  premium_price = 125
  drone_price = drone_ship_cost(weight)
  cast_weight = str(weight)
  if ground_price < premium_price and ground_price < drone_price:
    print("The cheapest shipping method for a package weighing " + cast_weight + "lbs is ground shipping. It will cost $" + 
  str(ground_ship_cost(weight)) + " to ship this package.")
  elif premium_price < ground_price and premium_price < drone_price:
    print("The cheapest shipping method for a package weighing " + cast_weight + "lbs is premium ground shipping. It will cost $125.00 to ship this package.")
  elif drone_price < ground_price and drone_price < premium_price: 
    print("The cheapest shipping method for a package weighing " + cast_weight + "lbs is drone shipping. It will cost $" + str(drone_ship_cost(weight)) + " to ship this package.")

print(calc_cheapest_option(4.8))
print(calc_cheapest_option(41.5))
3 Likes

There is even a better way to format output string in print statements inside calc_cheapest_option function is by using f-string (formatted string). Unfortunately, this concept isn’t taught at Codecademy at all. When using f-string, you put letter f in front of your string inside print statement, and put your variables inside curly braces. This is how your print statement inside if and elif can be re-written using f-string:

if ground_price < premium_price and ground_price < drone_price:
    print(f"The cheapest shipping method for a package weighing {cast_weight}lbs is ground shipping. It will cost ${ground_ship_cost(weight)} to ship this package.")
  elif premium_price < ground_price and premium_price < drone_price:
    print(f"The cheapest shipping method for a package weighing {cast_weight}lbs is premium ground shipping. It will cost $125.00 to ship this package.")
  elif drone_price < ground_price and drone_price < premium_price: 
    print(f"The cheapest shipping method for a package weighing {cast_weight}lbs is drone shipping. It will cost ${drone_ship_cost(weight)} to ship this package.")
3 Likes

Hey there! Congrats on finishing the project. You asked for ways to improve your code and the first thing that comes to mind is the logic of your conditionals. Check this out:

If weight > 10:
  cost = weight * 4.75
elif weight > 6:
  cost = weight * 4
elif weight > 2:
  cost = weight * 3
elif weight > 0:
  cost = weight * 1.50
else:
  return “Weight can’t be negative!”
  
return cost + flat_ground_charge

Since Python goes condition after condition and never checks the rest once it has found one that is True, you can cascade your logic! So if the weight is, let’s say, 7, the first if will fail and the second will work. Writing the conditionals this way is less wordy and easier to read :smiley:

3 Likes

That’s a bit of a weird one, as printf formatting is actually introduced on Codecademy… just not in the Python 3 material (last time I checked), which is where Sal’s Shipping is.

The Python 2 course introduces it fairly early on alongside introducing strings. There is information available on “printf formatting” in the Python docs if you wanted to learn about how it works. :slight_smile: