Sal's Shipping - 1st python program

Hi all,

This is my first post and these are my first part time weeks in Python.

Just did Sal’s Shipping as my 1st “program” and I got it to work.
But because I may bring with me a lot of vices from my years in MS Excel, If someone can look at my code and see if some of the writting is just the a wrong away to use the language or redudant ?

Thank you in advance.

def cheapest_shipping_method(weight):

  • if weight>10:*

  • return 4.75weight+20

  • elif weight>6:*

  • return 4.00weight+20

  • elif weight>2:*

  • return 3.00weight+20

  • else:*

  • return 1.50weight+20

def Ground_Shipping (weight):

  • price_GS_1= 1.50weight+20

  • price_GS_2= 3.00weight+20

  • price_GS_3= 4.00weight+20

  • price_GS_4= 4.75weight+20

  • if weight>10:*

  • return price_GS_4*

  • elif weight>6:*

  • return price_GS_3*

  • elif weight>2:*

  • return price_GS_2*

  • else:*

  • return price_GS_1*

premium_ground_shipping=125.00

def Drone_Shipping (weight):

  • price_DS_1= 4.50weight+0

  • price_DS_2= 9.00weight+0

  • price_DS_3= 12.00weight+0

  • price_DS_4= 14.25weight+0

  • if weight>10:*

  • return price_DS_4*

  • elif weight>6:*

  • return price_DS_3*

  • elif weight>2:*

  • return price_DS_2*

  • else:*

  • return price_DS_1*

print(Ground_Shipping(8.4))

print(Drone_Shipping(1.5))

print(premium_ground_shipping)

def comparative (weight):

  • if Ground_Shipping(weight)<= Drone_Shipping(weight) and Ground_Shipping(weight)<= premium_ground_shipping:*

  • return "Best Option: Ground Shipping for "+str(Ground_Shipping(weight))*

  • elif Drone_Shipping(weight)<= Ground_Shipping(weight) and Drone_Shipping(weight)<= premium_ground_shipping:*

  • return "Best Option: Drone Shipping for "+str(Drone_Shipping(weight))*

  • else:*

  • return "Best Option: Premium Ground Shipping for "+str(premium_ground_shipping)*

print(comparative (4.8))

print(comparative (8))

print(comparative (41.5))

It’s a little difficult to read the code without formatting, check the following FAQ which covers how to format code for the forums (amongst other things).

Congrats on having a look at something different. Hopefully you’ll soon start to see where even a simple script can make your workflow easier if you haven’t already.

As for some criticism- I can see a few places where there are a lot of repeated sections of code e.g. weight + 20 appears at eight separate locations in the code. Try to minimise sections where you repeat yourself (the DRY principle). I think a few of the variables could be simplified too (PRICE_GS_1 etc.), if you have logic that only needs to return a single price then perhaps you can calculate price individually (rather than every possible price but return just the one).

I think the logic statements in the final function could also be simplified a bit. Remember how control flows in an if statement, only one of these clauses should allow a return so you don’t need to check the full logic expression every time.

A final note would be start thinking about the names you are using for functions, variables etc. For example it’s hard to tell what a function called comparative might do, you have to read the code to find out (a little more explicit without being over the top might be good). Keep it in mind at least. Readability counts for a lot.

Thank you so much on your reply.

I will check the FAQs on that regard (at least).

So far I am just trying to absorb the maximum I can, since I don’t have any background on programming. But I love data, especially business related, and Python for Big Data will be on of the best options for me.

The “repeated sections” was something that I was looking at and thought to myself that something was wrong. I am still thinking in terms of Excel Tables, so I almost created in tables in the code.
I was happy that the number of lines used was close to the number on the video of the exercise, but you are right, and I really have to simplify on that.

About “start thinking about the names you are using for functions”, it seems algo an important tip. And I am
perfectionist on this kind of things, but on this case, I was just eager on resolving the problem. IT was truly a challenge as this is very new for me.

Again, thank you so much for your time spent.