Sal's Shipping: My Own Version

I decided to try the Sal’s Shipping project without reading any of the guiding instructions. After completing the project I decided to look at what the walkthrough came up with. Obviously very different.

I am curious to hear why my code is not optimal? Is it simply because editing the code in the future might be more tedious because there is a lack of variables?

Just to be clear, I’m not implying that my code should be the standard (I’m sure it’s pretty bad). I would just like to learn what about it is sub-optimal.

Thanks!

def calculate_cheapest_method(weight):
  if weight <= 2.3:
    print("Cheapest shipping method is Drone Shipping and costs " + str(weight * 4.50) + ". Thank you!")
  elif weight >= 2.4 and weight <= 6:
    print("Cheapest shipping method is Ground Shipping and costs " + str(weight * 3 + 20) + ". Thank you!")
  elif weight >= 6.1 and weight <= 10:
    print("Cheapest shipping method is Ground Shipping and costs " + str(weight * 4 + 20) + ". Thank you!")
  elif weight >= 10.1 and weight <= 22.1:
    print("Cheapest shipping method is Ground Shipping and costs " + str(weight * 4.75 + 20) + ". Thank you!")
  else:
    print("Cheapest shipping method is Premium ground shipping and costs $125.00. Thank you!")
    

Having hard-coded numbers in your logic coming out of nowhere is REALLY weird. Those numbers are based on data so they should be obtained from the data rather than be hard-coded. You did some math to obtain them, math that your program could be carrying out instead.

2 Likes

This is exactly the type of help/advice I was looking for. Thanks!

Actually have you understood the guided outcome?
Because I have not.
The part he is using:

print(
“The cheapest option available is $%.2f
with %s shipping.”
% (cost, method)
)

is super confusing to me.
First what is he doing with the % ? How does it work? Was it explained before and I have missed it?
and second: Hes referring to method and cost, both used in the defined in the method above and only there. I thought its not possible to access these variables outside of the defined method.
This guide is so confusing. Maybe someone can help?

1 Like

Welcome to the forums. :slight_smile:

It’s called printf-style formatting. I don’t know whether it’s specifically introduced in the Python 3 course. (It’s definitely in the Python 2 stuff.)

If you look closely, you’ll notice that the print statement is not outside the function. :slight_smile:

Ohhh… ty for replying.

I guess I have to study some python 2 too ^^
Is it actually wise to take the 2 course first or do you think I should finish 3 first?

No worries.

You don’t need to learn Python 2; that style of string formatting is present in Python 3 as well. The documentation like I put in my previous post will explain how to use it.

Realistically, I’d only recommend the Python 2 course if you were dead set on learning Python but weren’t a Pro subscriber for any reason.

If you’ve got access to the Python 3 course, as it would appear you do, then stick with Python 3. The differences between the two versions aren’t that great - if you can read/write Python 3 code then you’ll be fine if you ever run across Python 2 in the wild.

For people developing new Python libraries and programs, the recommendation is to use Python 3 wherever possible and there’s a lot of work being done to move Python 2 resources over to Python 3.

In short: the docs will explain that string formatting method, and stick with Python 3. :slight_smile:

TY your a great help ^^